<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>perlunicode - Unicode support in Perl - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/perlunicode">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlunicode.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/perlunicode">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/perlunicode">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/perlunicode">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/perlunicode">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/perlunicode">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/perlunicode">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/perlunicode">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/perlunicode">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/perlunicode">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/perlunicode">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/perlunicode">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/perlunicode">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/perlunicode">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/perlunicode">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/perlunicode">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/perlunicode">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/perlunicode">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/perlunicode">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/perlunicode">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/perlunicode">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/perlunicode">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/perlunicode">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/perlunicode">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/perlunicode">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/perlunicode">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/perlunicode">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/perlunicode">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/perlunicode">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/perlunicode">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/perlunicode">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/perlunicode">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/perlunicode">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/perlunicode">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/perlunicode">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/perlunicode">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/perlunicode">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/perlunicode">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/perlunicode">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/perlunicode">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/perlunicode">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/perlunicode">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/perlunicode">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/perlunicode">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/perlunicode">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/perlunicode">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/perlunicode">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/perlunicode">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/perlunicode">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/perlunicode">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/perlunicode">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/perlunicode">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/perlunicode">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/perlunicode">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/perlunicode">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/perlunicode">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/perlunicode">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/perlunicode">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/perlunicode">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/perlunicode">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/perlunicode">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/perlunicode">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/perlunicode">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/perlunicode">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/perlunicode">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/perlunicode">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/perlunicode">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/perlunicode">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/perlunicode">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/perlunicode">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/perlunicode">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/perlunicode">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/perlunicode">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/perlunicode">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/perlunicode">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/perlunicode">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/perlunicode">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/perlunicode">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/perlunicode">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/perlunicode">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/perlunicode">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlunicode.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/perlunicode">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/perlunicode">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/perlunicode">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/perlunicode">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/perlunicode">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/perlunicode">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/perlunicode">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/perlunicode">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/perlunicode">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/perlunicode">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/perlunicode">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/perlunicode">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/perlunicode">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/perlunicode">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/perlunicode">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/perlunicode">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/perlunicode">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/perlunicode">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/perlunicode">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/perlunicode">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/perlunicode">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/perlunicode">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/perlunicode">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/perlunicode">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/perlunicode">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/perlunicode">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/perlunicode">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/perlunicode">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/perlunicode">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/perlunicode">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/perlunicode">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/perlunicode">5.35.1</a>
          <a class="dropdown-item active" href="perlunicode.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlunicode.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="perlunicode.html">perlunicode</a>
          <div id="more">
            (<a href="perlunicode.txt">source</a>,
            <a href="https://metacpan.org/pod/perlunicode">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#DESCRIPTION">DESCRIPTION</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Important-Caveats">Important Caveats</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Byte-and-Character-Semantics">Byte and Character Semantics</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#ASCII-Rules-versus-Unicode-Rules">ASCII Rules versus Unicode Rules</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Extended-Grapheme-Clusters-(Logical-characters)">Extended Grapheme Clusters (Logical characters)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Unicode-Character-Properties">Unicode Character Properties</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#General_Category">General_Category</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Bidirectional-Character-Types">Bidirectional Character Types</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Scripts">Scripts</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Use-of-the-%22Is%22-Prefix">Use of the &quot;Is&quot; Prefix</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Blocks">Blocks</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Other-Properties">Other Properties</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Comparison-of-%5CN%7B...%7D-and-%5Cp%7Bname=...%7D">Comparison of \N{...} and \p{name=...}</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Wildcards-in-Property-Values">Wildcards in Property Values</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#User-Defined-Character-Properties">User-Defined Character Properties</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#User-Defined-Case-Mappings-(for-serious-hackers-only)">User-Defined Case Mappings (for serious hackers only)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Character-Encodings-for-Input-and-Output">Character Encodings for Input and Output</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Unicode-Regular-Expression-Support-Level">Unicode Regular Expression Support Level</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Level-1-Basic-Unicode-Support">Level 1 - Basic Unicode Support</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Level-2-Extended-Unicode-Support">Level 2 - Extended Unicode Support</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Level-3-Tailored-Support">Level 3 - Tailored Support</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Unicode-Encodings">Unicode Encodings</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Noncharacter-code-points">Noncharacter code points</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Beyond-Unicode-code-points">Beyond Unicode code points</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Security-Implications-of-Unicode">Security Implications of Unicode</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Unicode-in-Perl-on-EBCDIC">Unicode in Perl on EBCDIC</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Locales">Locales</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#When-Unicode-Does-Not-Happen">When Unicode Does Not Happen</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#The-%22Unicode-Bug%22">The &quot;Unicode Bug&quot;</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Forcing-Unicode-in-Perl-(Or-Unforcing-Unicode-in-Perl)">Forcing Unicode in Perl (Or Unforcing Unicode in Perl)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Using-Unicode-in-XS">Using Unicode in XS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Hacking-Perl-to-work-on-earlier-Unicode-versions-(for-very-serious-hackers-only)">Hacking Perl to work on earlier Unicode versions (for very serious hackers only)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Porting-code-from-perl-5.6.X">Porting code from perl-5.6.X</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#BUGS">BUGS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Interaction-with-Extensions">Interaction with Extensions</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#Speed">Speed</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlunicode.html#SEE-ALSO">SEE ALSO</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="perlunicode.html#NAME">#</a>NAME</h1>

<p>perlunicode - Unicode support in Perl</p>

<h1 id="DESCRIPTION"><a class="permalink" href="perlunicode.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>If you haven&#39;t already, before reading this document, you should become familiar with both <a href="perlunitut.html">perlunitut</a> and <a href="perluniintro.html">perluniintro</a>.</p>

<p>Unicode aims to <b>UNI</b>-fy the en-<b>CODE</b>-ings of all the world&#39;s character sets into a single Standard. For quite a few of the various coding standards that existed when Unicode was first created, converting from each to Unicode essentially meant adding a constant to each code point in the original standard, and converting back meant just subtracting that same constant. For ASCII and ISO-8859-1, the constant is 0. For ISO-8859-5, (Cyrillic) the constant is 864; for Hebrew (ISO-8859-8), it&#39;s 1488; Thai (ISO-8859-11), 3424; and so forth. This made it easy to do the conversions, and facilitated the adoption of Unicode.</p>

<p>And it worked; nowadays, those legacy standards are rarely used. Most everyone uses Unicode.</p>

<p>Unicode is a comprehensive standard. It specifies many things outside the scope of Perl, such as how to display sequences of characters. For a full discussion of all aspects of Unicode, see <a href="https://www.unicode.org">https://www.unicode.org</a>.</p>

<h2 id="Important-Caveats"><a class="permalink" href="perlunicode.html#Important-Caveats">#</a><a id="Important"></a>Important Caveats</h2>

<p>Even though some of this section may not be understandable to you on first reading, we think it&#39;s important enough to highlight some of the gotchas before delving further, so here goes:</p>

<p>Unicode support is an extensive requirement. While Perl does not implement the Unicode standard or the accompanying technical reports from cover to cover, Perl does support many Unicode features.</p>

<p>Also, the use of Unicode may present security issues that aren&#39;t obvious, see <a href="perlunicode.html#Security-Implications-of-Unicode">&quot;Security Implications of Unicode&quot;</a> below.</p>

<dl>

<dt id="Safest-if-you-use-feature-&#39;unicode_strings&#39;"><a class="permalink" href="perlunicode.html#Safest-if-you-use-feature-&#39;unicode_strings&#39;">#</a><a id="Safest"></a><a id="Safest-if-you-use-feature-unicode_strings"></a>Safest if you <code>use feature &#39;unicode_strings&#39;</code></dt>
<dd>

<p>In order to preserve backward compatibility, Perl does not turn on full internal Unicode support unless the pragma <a href="feature.html#The-%27unicode_strings%27-feature"><span style="white-space: nowrap;"><code>use feature &#39;unicode_strings&#39;</code></span></a> is specified. (This is automatically selected if you <span style="white-space: nowrap;"><code>use 5.012</code></span> or higher.) Failure to do this can trigger unexpected surprises. See <a href="perlunicode.html#The-%22Unicode-Bug%22">&quot;The &quot;Unicode Bug&quot;&quot;</a> below.</p>

<p>This pragma doesn&#39;t affect I/O. Nor does it change the internal representation of strings, only their interpretation. There are still several places where Unicode isn&#39;t fully supported, such as in filenames.</p>

</dd>
<dt id="Input-and-Output-Layers"><a class="permalink" href="perlunicode.html#Input-and-Output-Layers">#</a><a id="Input"></a>Input and Output Layers</dt>
<dd>

<p>Use the <code>:encoding(...)</code> layer to read from and write to filehandles using the specified encoding. (See <a href="open.html">open</a>.)</p>

</dd>
<dt id="You-must-convert-your-non-ASCII,-non-UTF-8-Perl-scripts-to-be-UTF-8."><a class="permalink" href="perlunicode.html#You-must-convert-your-non-ASCII,-non-UTF-8-Perl-scripts-to-be-UTF-8.">#</a><a id="You"></a><a id="You-must-convert-your-non-ASCII-non-UTF-8-Perl-scripts-to-be-UTF-8"></a>You must convert your non-ASCII, non-UTF-8 Perl scripts to be UTF-8.</dt>
<dd>

<p>The <a href="encoding.html">encoding</a> module has been deprecated since perl 5.18 and the perl internals it requires have been removed with perl 5.26.</p>

</dd>
<dt id="use-utf8-still-needed-to-enable-UTF-8-in-scripts"><a class="permalink" href="perlunicode.html#use-utf8-still-needed-to-enable-UTF-8-in-scripts">#</a><a id="use"></a><code>use utf8</code> still needed to enable <a href="perlunicode.html#Unicode-Encodings">UTF-8</a> in scripts</dt>
<dd>

<p>If your Perl script is itself encoded in <a href="perlunicode.html#Unicode-Encodings">UTF-8</a>, the <span style="white-space: nowrap;"><code>use utf8</code></span> pragma must be explicitly included to enable recognition of that (in string or regular expression literals, or in identifier names). <b>This is the only time when an explicit <span style="white-space: nowrap;"><code>use utf8</code></span> is needed.</b> (See <a href="utf8.html">utf8</a>).</p>

<p>If a Perl script begins with the bytes that form the UTF-8 encoding of the Unicode BYTE ORDER MARK (<code>BOM</code>, see <a href="perlunicode.html#Unicode-Encodings">&quot;Unicode Encodings&quot;</a>), those bytes are completely ignored.</p>

</dd>
<dt id="UTF-16-scripts-autodetected"><a class="permalink" href="perlunicode.html#UTF-16-scripts-autodetected">#</a><a id="UTF"></a><a href="perlunicode.html#Unicode-Encodings">UTF-16</a> scripts autodetected</dt>
<dd>

<p>If a Perl script begins with the Unicode <code>BOM</code> (UTF-16LE, UTF16-BE), or if the script looks like non-<code>BOM</code>-marked UTF-16 of either endianness, Perl will correctly read in the script as the appropriate Unicode encoding.</p>

</dd>
</dl>

<h2 id="Byte-and-Character-Semantics"><a class="permalink" href="perlunicode.html#Byte-and-Character-Semantics">#</a><a id="Byte"></a>Byte and Character Semantics</h2>

<p>Before Unicode, most encodings used 8 bits (a single byte) to encode each character. Thus a character was a byte, and a byte was a character, and there could be only 256 or fewer possible characters. &quot;Byte Semantics&quot; in the title of this section refers to this behavior. There was no need to distinguish between &quot;Byte&quot; and &quot;Character&quot;.</p>

<p>Then along comes Unicode which has room for over a million characters (and Perl allows for even more). This means that a character may require more than a single byte to represent it, and so the two terms are no longer equivalent. What matter are the characters as whole entities, and not usually the bytes that comprise them. That&#39;s what the term &quot;Character Semantics&quot; in the title of this section refers to.</p>

<p>Perl had to change internally to decouple &quot;bytes&quot; from &quot;characters&quot;. It is important that you too change your ideas, if you haven&#39;t already, so that &quot;byte&quot; and &quot;character&quot; no longer mean the same thing in your mind.</p>

<p>The basic building block of Perl strings has always been a &quot;character&quot;. The changes basically come down to that the implementation no longer thinks that a character is always just a single byte.</p>

<p>There are various things to note:</p>

<ul>

<li><p>String handling functions, for the most part, continue to operate in terms of characters. <code>length()</code>, for example, returns the number of characters in a string, just as before. But that number no longer is necessarily the same as the number of bytes in the string (there may be more bytes than characters). The other such functions include <code>chop()</code>, <code>chomp()</code>, <code>substr()</code>, <code>pos()</code>, <code>index()</code>, <code>rindex()</code>, <code>sort()</code>, <code>sprintf()</code>, and <code>write()</code>.</p>

<p>The exceptions are:</p>

<ul>

<li><p>the bit-oriented <code>vec</code></p>

<p> </p>

</li>
<li><p>the byte-oriented <code>pack</code>/<code>unpack</code> <code>&quot;C&quot;</code> format</p>

<p>However, the <code>W</code> specifier does operate on whole characters, as does the <code>U</code> specifier.</p>

</li>
<li><p>some operators that interact with the platform&#39;s operating system</p>

<p>Operators dealing with filenames are examples.</p>

</li>
<li><p>when the functions are called from within the scope of the <span style="white-space: nowrap;"><code><a href="bytes.html">use bytes</a></code></span> pragma</p>

<p>Likely, you should use this only for debugging anyway.</p>

</li>
</ul>

</li>
<li><p>Strings--including hash keys--and regular expression patterns may contain characters that have ordinal values larger than 255.</p>

<p>If you use a Unicode editor to edit your program, Unicode characters may occur directly within the literal strings in UTF-8 encoding, or UTF-16. (The former requires a <code>use utf8</code>, the latter may require a <code>BOM</code>.)</p>

<p><a href="perluniintro.html#Creating-Unicode">&quot;Creating Unicode&quot; in perluniintro</a> gives other ways to place non-ASCII characters in your strings.</p>

</li>
<li><p>The <code>chr()</code> and <code>ord()</code> functions work on whole characters.</p>

</li>
<li><p>Regular expressions match whole characters. For example, <code>&quot;.&quot;</code> matches a whole character instead of only a single byte.</p>

</li>
<li><p>The <code>tr///</code> operator translates whole characters. (Note that the <code>tr///CU</code> functionality has been removed. For similar functionality to that, see <code>pack(&#39;U0&#39;, ...)</code> and <code>pack(&#39;C0&#39;, ...)</code>).</p>

</li>
<li><p><code>scalar reverse()</code> reverses by character rather than by byte.</p>

</li>
<li><p>The bit string operators, <code>&amp; | ^ ~</code> and (starting in v5.22) <code>&amp;. |. ^. ~.</code> can operate on bit strings encoded in UTF-8, but this can give unexpected results if any of the strings contain code points above 0xFF. Starting in v5.28, it is a fatal error to have such an operand. Otherwise, the operation is performed on a non-UTF-8 copy of the operand. If you&#39;re not sure about the encoding of a string, downgrade it before using any of these operators; you can use <a href="utf8.html#Utility-functions"><code>utf8::utf8_downgrade()</code></a>.</p>

</li>
</ul>

<p>The bottom line is that Perl has always practiced &quot;Character Semantics&quot;, but with the advent of Unicode, that is now different than &quot;Byte Semantics&quot;.</p>

<h2 id="ASCII-Rules-versus-Unicode-Rules"><a class="permalink" href="perlunicode.html#ASCII-Rules-versus-Unicode-Rules">#</a><a id="ASCII"></a>ASCII Rules versus Unicode Rules</h2>

<p>Before Unicode, when a character was a byte was a character, Perl knew only about the 128 characters defined by ASCII, code points 0 through 127 (except for under <a href="perllocale.html"><span style="white-space: nowrap;"><code>use locale</code></span></a>). That left the code points 128 to 255 as unassigned, and available for whatever use a program might want. The only semantics they have is their ordinal numbers, and that they are members of none of the non-negative character classes. None are considered to match <code>\w</code> for example, but all match <code>\W</code>.</p>

<p>Unicode, of course, assigns each of those code points a particular meaning (along with ones above 255). To preserve backward compatibility, Perl only uses the Unicode meanings when there is some indication that Unicode is what is intended; otherwise the non-ASCII code points remain treated as if they are unassigned.</p>

<p>Here are the ways that Perl knows that a string should be treated as Unicode:</p>

<ul>

<li><p>Within the scope of <span style="white-space: nowrap;"><code>use utf8</code></span></p>

<p>If the whole program is Unicode (signified by using 8-bit <b>U</b>nicode <b>T</b>ransformation <b>F</b>ormat), then all literal strings within it must be Unicode.</p>

</li>
<li><p>Within the scope of <a href="feature.html#The-%27unicode_strings%27-feature"><span style="white-space: nowrap;"><code>use feature &#39;unicode_strings&#39;</code></span></a></p>

<p>This pragma was created so you can explicitly tell Perl that operations executed within its scope are to use Unicode rules. More operations are affected with newer perls. See <a href="perlunicode.html#The-%22Unicode-Bug%22">&quot;The &quot;Unicode Bug&quot;&quot;</a>.</p>

</li>
<li><p>Within the scope of <span style="white-space: nowrap;"><code>use 5.012</code></span> or higher</p>

<p>This implicitly turns on <span style="white-space: nowrap;"><code>use feature &#39;unicode_strings&#39;</code></span>.</p>

</li>
<li><p>Within the scope of <a href="perllocale.html#Unicode-and-UTF-8"><span style="white-space: nowrap;"><code>use locale &#39;not_characters&#39;</code></span></a>, or <a href="perllocale.html"><span style="white-space: nowrap;"><code>use locale</code></span></a> and the current locale is a UTF-8 locale.</p>

<p>The former is defined to imply Unicode handling; and the latter indicates a Unicode locale, hence a Unicode interpretation of all strings within it.</p>

</li>
<li><p>When the string contains a Unicode-only code point</p>

<p>Perl has never accepted code points above 255 without them being Unicode, so their use implies Unicode for the whole string.</p>

</li>
<li><p>When the string contains a Unicode named code point <code>\N{...}</code></p>

<p>The <code>\N{...}</code> construct explicitly refers to a Unicode code point, even if it is one that is also in ASCII. Therefore the string containing it must be Unicode.</p>

</li>
<li><p>When the string has come from an external source marked as Unicode</p>

<p>The <a href="perlrun.html#-C-%5Bnumber%2Flist%5D"><code>-C</code></a> command line option can specify that certain inputs to the program are Unicode, and the values of this can be read by your Perl code, see <a href="perlvar.html#%24%7B%5EUNICODE%7D">&quot;${^UNICODE}&quot; in perlvar</a>.</p>

</li>
<li><p>When the string has been upgraded to UTF-8</p>

<p>The function <a href="utf8.html#Utility-functions"><code>utf8::utf8_upgrade()</code></a> can be explicitly used to permanently (unless a subsequent <code>utf8::utf8_downgrade()</code> is called) cause a string to be treated as Unicode.</p>

</li>
<li><p>There are additional methods for regular expression patterns</p>

<p>A pattern that is compiled with the <code>/u</code> or <code>/a</code> modifiers is treated as Unicode (though there are some restrictions with <code>/a</code>). Under the <code>/d</code> and <code>/l</code> modifiers, there are several other indications for Unicode; see <a href="perlre.html#Character-set-modifiers">&quot;Character set modifiers&quot; in perlre</a>.</p>

</li>
</ul>

<p>Note that all of the above are overridden within the scope of <code><a href="bytes.html">use bytes</a></code>; but you should be using this pragma only for debugging.</p>

<p>Note also that some interactions with the platform&#39;s operating system never use Unicode rules.</p>

<p>When Unicode rules are in effect:</p>

<ul>

<li><p>Case translation operators use the Unicode case translation tables.</p>

<p>Note that <code>uc()</code>, or <code>\U</code> in interpolated strings, translates to uppercase, while <code>ucfirst</code>, or <code>\u</code> in interpolated strings, translates to titlecase in languages that make the distinction (which is equivalent to uppercase in languages without the distinction).</p>

<p>There is a CPAN module, <code><a href="./Unicode::Casing.html">Unicode::Casing</a></code>, which allows you to define your own mappings to be used in <code>lc()</code>, <code>lcfirst()</code>, <code>uc()</code>, <code>ucfirst()</code>, and <code>fc</code> (or their double-quoted string inlined versions such as <code>\U</code>). (Prior to Perl 5.16, this functionality was partially provided in the Perl core, but suffered from a number of insurmountable drawbacks, so the CPAN module was written instead.)</p>

</li>
<li><p>Character classes in regular expressions match based on the character properties specified in the Unicode properties database.</p>

<p><code>\w</code> can be used to match a Japanese ideograph, for instance; and <code>[[:digit:]]</code> a Bengali number.</p>

</li>
<li><p>Named Unicode properties, scripts, and block ranges may be used (like bracketed character classes) by using the <code>\p{}</code> &quot;matches property&quot; construct and the <code>\P{}</code> negation, &quot;doesn&#39;t match property&quot;.</p>

<p>See <a href="perlunicode.html#Unicode-Character-Properties">&quot;Unicode Character Properties&quot;</a> for more details.</p>

<p>You can define your own character properties and use them in the regular expression with the <code>\p{}</code> or <code>\P{}</code> construct. See <a href="perlunicode.html#User-Defined-Character-Properties">&quot;User-Defined Character Properties&quot;</a> for more details.</p>

</li>
</ul>

<h2 id="Extended-Grapheme-Clusters-(Logical-characters)"><a class="permalink" href="perlunicode.html#Extended-Grapheme-Clusters-(Logical-characters)">#</a><a id="Extended"></a><a id="Extended-Grapheme-Clusters-Logical-characters"></a>Extended Grapheme Clusters (Logical characters)</h2>

<p>Consider a character, say <code>H</code>. It could appear with various marks around it, such as an acute accent, or a circumflex, or various hooks, circles, arrows, <i>etc.</i>, above, below, to one side or the other, <i>etc</i>. There are many possibilities among the world&#39;s languages. The number of combinations is astronomical, and if there were a character for each combination, it would soon exhaust Unicode&#39;s more than a million possible characters. So Unicode took a different approach: there is a character for the base <code>H</code>, and a character for each of the possible marks, and these can be variously combined to get a final logical character. So a logical character--what appears to be a single character--can be a sequence of more than one individual characters. The Unicode standard calls these &quot;extended grapheme clusters&quot; (which is an improved version of the no-longer much used &quot;grapheme cluster&quot;); Perl furnishes the <code>\X</code> regular expression construct to match such sequences in their entirety.</p>

<p>But Unicode&#39;s intent is to unify the existing character set standards and practices, and several pre-existing standards have single characters that mean the same thing as some of these combinations, like ISO-8859-1, which has quite a few of them. For example, <code>&quot;LATIN CAPITAL LETTER E WITH ACUTE&quot;</code> was already in this standard when Unicode came along. Unicode therefore added it to its repertoire as that single character. But this character is considered by Unicode to be equivalent to the sequence consisting of the character <code>&quot;LATIN CAPITAL LETTER E&quot;</code> followed by the character <code>&quot;COMBINING ACUTE ACCENT&quot;</code>.</p>

<p><code>&quot;LATIN CAPITAL LETTER E WITH ACUTE&quot;</code> is called a &quot;pre-composed&quot; character, and its equivalence with the &quot;E&quot; and the &quot;COMBINING ACCENT&quot; sequence is called canonical equivalence. All pre-composed characters are said to have a decomposition (into the equivalent sequence), and the decomposition type is also called canonical. A string may be comprised as much as possible of precomposed characters, or it may be comprised of entirely decomposed characters. Unicode calls these respectively, &quot;Normalization Form Composed&quot; (NFC) and &quot;Normalization Form Decomposed&quot;. The <code><a href="./Unicode::Normalize.html">Unicode::Normalize</a></code> module contains functions that convert between the two. A string may also have both composed characters and decomposed characters; this module can be used to make it all one or the other.</p>

<p>You may be presented with strings in any of these equivalent forms. There is currently nothing in Perl 5 that ignores the differences. So you&#39;ll have to specially handle it. The usual advice is to convert your inputs to <code>NFD</code> before processing further.</p>

<p>For more detailed information, see <a href="http://unicode.org/reports/tr15/">http://unicode.org/reports/tr15/</a>.</p>

<h2 id="Unicode-Character-Properties"><a class="permalink" href="perlunicode.html#Unicode-Character-Properties">#</a><a id="Unicode"></a>Unicode Character Properties</h2>

<p>(The only time that Perl considers a sequence of individual code points as a single logical character is in the <code>\X</code> construct, already mentioned above. Therefore &quot;character&quot; in this discussion means a single Unicode code point.)</p>

<p>Very nearly all Unicode character properties are accessible through regular expressions by using the <code>\p{}</code> &quot;matches property&quot; construct and the <code>\P{}</code> &quot;doesn&#39;t match property&quot; for its negation.</p>

<p>For instance, <code>\p{Uppercase}</code> matches any single character with the Unicode <code>&quot;Uppercase&quot;</code> property, while <code>\p{L}</code> matches any character with a <code>General_Category</code> of <code>&quot;L&quot;</code> (letter) property (see <a href="perlunicode.html#General_Category">&quot;General_Category&quot;</a> below). Brackets are not required for single letter property names, so <code>\p{L}</code> is equivalent to <code>\pL</code>.</p>

<p>More formally, <code>\p{Uppercase}</code> matches any single character whose Unicode <code>Uppercase</code> property value is <code>True</code>, and <code>\P{Uppercase}</code> matches any character whose <code>Uppercase</code> property value is <code>False</code>, and they could have been written as <code>\p{Uppercase=True}</code> and <code>\p{Uppercase=False}</code>, respectively.</p>

<p>This formality is needed when properties are not binary; that is, if they can take on more values than just <code>True</code> and <code>False</code>. For example, the <code>Bidi_Class</code> property (see <a href="perlunicode.html#Bidirectional-Character-Types">&quot;Bidirectional Character Types&quot;</a> below), can take on several different values, such as <code>Left</code>, <code>Right</code>, <code>Whitespace</code>, and others. To match these, one needs to specify both the property name (<code>Bidi_Class</code>), AND the value being matched against (<code>Left</code>, <code>Right</code>, <i>etc.</i>). This is done, as in the examples above, by having the two components separated by an equal sign (or interchangeably, a colon), like <code>\p{Bidi_Class: Left}</code>.</p>

<p>All Unicode-defined character properties may be written in these compound forms of <code>\p{<i>property</i>=<i>value</i>}</code> or <code>\p{<i>property</i>:<i>value</i>}</code>, but Perl provides some additional properties that are written only in the single form, as well as single-form short-cuts for all binary properties and certain others described below, in which you may omit the property name and the equals or colon separator.</p>

<p>Most Unicode character properties have at least two synonyms (or aliases if you prefer): a short one that is easier to type and a longer one that is more descriptive and hence easier to understand. Thus the <code>&quot;L&quot;</code> and <code>&quot;Letter&quot;</code> properties above are equivalent and can be used interchangeably. Likewise, <code>&quot;Upper&quot;</code> is a synonym for <code>&quot;Uppercase&quot;</code>, and we could have written <code>\p{Uppercase}</code> equivalently as <code>\p{Upper}</code>. Also, there are typically various synonyms for the values the property can be. For binary properties, <code>&quot;True&quot;</code> has 3 synonyms: <code>&quot;T&quot;</code>, <code>&quot;Yes&quot;</code>, and <code>&quot;Y&quot;</code>; and <code>&quot;False&quot;</code> has correspondingly <code>&quot;F&quot;</code>, <code>&quot;No&quot;</code>, and <code>&quot;N&quot;</code>. But be careful. A short form of a value for one property may not mean the same thing as the short form spelled the same for another. Thus, for the <code><a href="perlunicode.html#General_Category">&quot;General_Category&quot;</a></code> property, <code>&quot;L&quot;</code> means <code>&quot;Letter&quot;</code>, but for the <a href="perlunicode.html#Bidirectional-Character-Types"><code>Bidi_Class</code></a> property, <code>&quot;L&quot;</code> means <code>&quot;Left&quot;</code>. A complete list of properties and synonyms is in <a href="perluniprops.html">perluniprops</a>.</p>

<p>Upper/lower case differences in property names and values are irrelevant; thus <code>\p{Upper}</code> means the same thing as <code>\p{upper}</code> or even <code>\p{UpPeR}</code>. Similarly, you can add or subtract underscores anywhere in the middle of a word, so that these are also equivalent to <code>\p{U_p_p_e_r}</code>. And white space is generally irrelevant adjacent to non-word characters, such as the braces and the equals or colon separators, so <code>\p{ Upper }</code> and <code>\p{ Upper_case : Y }</code> are equivalent to these as well. In fact, white space and even hyphens can usually be added or deleted anywhere. So even <code>\p{ Up-per case = Yes}</code> is equivalent. All this is called &quot;loose-matching&quot; by Unicode. The &quot;name&quot; property has some restrictions on this due to a few outlier names. Full details are given in <a href="https://www.unicode.org/reports/tr44/tr44-24.html#UAX44-LM2">https://www.unicode.org/reports/tr44/tr44-24.html#UAX44-LM2</a>.</p>

<p>The few places where stricter matching is used is in the middle of numbers, the &quot;name&quot; property, and in the Perl extension properties that begin or end with an underscore. Stricter matching cares about white space (except adjacent to non-word characters), hyphens, and non-interior underscores.</p>

<p>You can also use negation in both <code>\p{}</code> and <code>\P{}</code> by introducing a caret (<code>^</code>) between the first brace and the property name: <code>\p{^Tamil}</code> is equal to <code>\P{Tamil}</code>.</p>

<p>Almost all properties are immune to case-insensitive matching. That is, adding a <code>/i</code> regular expression modifier does not change what they match. There are two sets that are affected. The first set is <code>Uppercase_Letter</code>, <code>Lowercase_Letter</code>, and <code>Titlecase_Letter</code>, all of which match <code>Cased_Letter</code> under <code>/i</code> matching. And the second set is <code>Uppercase</code>, <code>Lowercase</code>, and <code>Titlecase</code>, all of which match <code>Cased</code> under <code>/i</code> matching. This set also includes its subsets <code>PosixUpper</code> and <code>PosixLower</code> both of which under <code>/i</code> match <code>PosixAlpha</code>. (The difference between these sets is that some things, such as Roman numerals, come in both upper and lower case so they are <code>Cased</code>, but aren&#39;t considered letters, so they aren&#39;t <code>Cased_Letter</code>&#39;s.)</p>

<p>See <a href="perlunicode.html#Beyond-Unicode-code-points">&quot;Beyond Unicode code points&quot;</a> for special considerations when matching Unicode properties against non-Unicode code points.</p>

<h3 id="General_Category"><a class="permalink" href="perlunicode.html#General_Category">#</a><b>General_Category</b></h3>

<p>Every Unicode character is assigned a general category, which is the &quot;most usual categorization of a character&quot; (from <a href="https://www.unicode.org/reports/tr44">https://www.unicode.org/reports/tr44</a>).</p>

<p>The compound way of writing these is like <code>\p{General_Category=Number}</code> (short: <code>\p{gc:n}</code>). But Perl furnishes shortcuts in which everything up through the equal or colon separator is omitted. So you can instead just write <code>\pN</code>.</p>

<p>Here are the short and long forms of the values the <code>General Category</code> property can have:</p>

<pre><code class="plaintext">Short       Long

L           Letter
LC, L&amp;      Cased_Letter (that is: [\p{Ll}\p{Lu}\p{Lt}])
Lu          Uppercase_Letter
Ll          Lowercase_Letter
Lt          Titlecase_Letter
Lm          Modifier_Letter
Lo          Other_Letter

M           Mark
Mn          Nonspacing_Mark
Mc          Spacing_Mark
Me          Enclosing_Mark

N           Number
Nd          Decimal_Number (also Digit)
Nl          Letter_Number
No          Other_Number

P           Punctuation (also Punct)
Pc          Connector_Punctuation
Pd          Dash_Punctuation
Ps          Open_Punctuation
Pe          Close_Punctuation
Pi          Initial_Punctuation
            (may behave like Ps or Pe depending on usage)
Pf          Final_Punctuation
            (may behave like Ps or Pe depending on usage)
Po          Other_Punctuation

S           Symbol
Sm          Math_Symbol
Sc          Currency_Symbol
Sk          Modifier_Symbol
So          Other_Symbol

Z           Separator
Zs          Space_Separator
Zl          Line_Separator
Zp          Paragraph_Separator

C           Other
Cc          Control (also Cntrl)
Cf          Format
Cs          Surrogate
Co          Private_Use
Cn          Unassigned</code></pre>

<p>Single-letter properties match all characters in any of the two-letter sub-properties starting with the same letter. <code>LC</code> and <code>L&amp;</code> are special: both are aliases for the set consisting of everything matched by <code>Ll</code>, <code>Lu</code>, and <code>Lt</code>.</p>

<h3 id="Bidirectional-Character-Types"><a class="permalink" href="perlunicode.html#Bidirectional-Character-Types">#</a><a id="Bidirectional"></a><b>Bidirectional Character Types</b></h3>

<p>Because scripts differ in their directionality (Hebrew and Arabic are written right to left, for example) Unicode supplies a <code>Bidi_Class</code> property. Some of the values this property can have are:</p>

<pre><code class="plaintext">Value       Meaning

L           Left-to-Right
LRE         Left-to-Right Embedding
LRO         Left-to-Right Override
R           Right-to-Left
AL          Arabic Letter
RLE         Right-to-Left Embedding
RLO         Right-to-Left Override
PDF         Pop Directional Format
EN          European Number
ES          European Separator
ET          European Terminator
AN          Arabic Number
CS          Common Separator
NSM         Non-Spacing Mark
BN          Boundary Neutral
B           Paragraph Separator
S           Segment Separator
WS          Whitespace
ON          Other Neutrals</code></pre>

<p>This property is always written in the compound form. For example, <code>\p{Bidi_Class:R}</code> matches characters that are normally written right to left. Unlike the <code><a href="perlunicode.html#General_Category">&quot;General_Category&quot;</a></code> property, this property can have more values added in a future Unicode release. Those listed above comprised the complete set for many Unicode releases, but others were added in Unicode 6.3; you can always find what the current ones are in <a href="perluniprops.html">perluniprops</a>. And <a href="https://www.unicode.org/reports/tr9/">https://www.unicode.org/reports/tr9/</a> describes how to use them.</p>

<h3 id="Scripts"><a class="permalink" href="perlunicode.html#Scripts">#</a><b>Scripts</b></h3>

<p>The world&#39;s languages are written in many different scripts. This sentence (unless you&#39;re reading it in translation) is written in Latin, while Russian is written in Cyrillic, and Greek is written in, well, Greek; Japanese mainly in Hiragana or Katakana. There are many more.</p>

<p>The Unicode <code>Script</code> and <code>Script_Extensions</code> properties give what script a given character is in. The <code>Script_Extensions</code> property is an improved version of <code>Script</code>, as demonstrated below. Either property can be specified with the compound form like <code>\p{Script=Hebrew}</code> (short: <code>\p{sc=hebr}</code>), or <code>\p{Script_Extensions=Javanese}</code> (short: <code>\p{scx=java}</code>). In addition, Perl furnishes shortcuts for all <code>Script_Extensions</code> property names. You can omit everything up through the equals (or colon), and simply write <code>\p{Latin}</code> or <code>\P{Cyrillic}</code>. (This is not true for <code>Script</code>, which is required to be written in the compound form. Prior to Perl v5.26, the single form returned the plain old <code>Script</code> version, but was changed because <code>Script_Extensions</code> gives better results.)</p>

<p>The difference between these two properties involves characters that are used in multiple scripts. For example the digits &#39;0&#39; through &#39;9&#39; are used in many parts of the world. These are placed in a script named <code>Common</code>. Other characters are used in just a few scripts. For example, the <code>&quot;KATAKANA-HIRAGANA DOUBLE HYPHEN&quot;</code> is used in both Japanese scripts, Katakana and Hiragana, but nowhere else. The <code>Script</code> property places all characters that are used in multiple scripts in the <code>Common</code> script, while the <code>Script_Extensions</code> property places those that are used in only a few scripts into each of those scripts; while still using <code>Common</code> for those used in many scripts. Thus both these match:</p>

<pre><code class="plaintext">&quot;0&quot; =~ /\p{sc=Common}/     # Matches
&quot;0&quot; =~ /\p{scx=Common}/    # Matches</code></pre>

<p>and only the first of these match:</p>

<pre><code class="plaintext">&quot;\N{KATAKANA-HIRAGANA DOUBLE HYPHEN}&quot; =~ /\p{sc=Common}  # Matches
&quot;\N{KATAKANA-HIRAGANA DOUBLE HYPHEN}&quot; =~ /\p{scx=Common} # No match</code></pre>

<p>And only the last two of these match:</p>

<pre><code class="plaintext">&quot;\N{KATAKANA-HIRAGANA DOUBLE HYPHEN}&quot; =~ /\p{sc=Hiragana}  # No match
&quot;\N{KATAKANA-HIRAGANA DOUBLE HYPHEN}&quot; =~ /\p{sc=Katakana}  # No match
&quot;\N{KATAKANA-HIRAGANA DOUBLE HYPHEN}&quot; =~ /\p{scx=Hiragana} # Matches
&quot;\N{KATAKANA-HIRAGANA DOUBLE HYPHEN}&quot; =~ /\p{scx=Katakana} # Matches</code></pre>

<p><code>Script_Extensions</code> is thus an improved <code>Script</code>, in which there are fewer characters in the <code>Common</code> script, and correspondingly more in other scripts. It is new in Unicode version 6.0, and its data are likely to change significantly in later releases, as things get sorted out. New code should probably be using <code>Script_Extensions</code> and not plain <code>Script</code>. If you compile perl with a Unicode release that doesn&#39;t have <code>Script_Extensions</code>, the single form Perl extensions will instead refer to the plain <code>Script</code> property. If you compile with a version of Unicode that doesn&#39;t have the <code>Script</code> property, these extensions will not be defined at all.</p>

<p>(Actually, besides <code>Common</code>, the <code>Inherited</code> script, contains characters that are used in multiple scripts. These are modifier characters which inherit the script value of the controlling character. Some of these are used in many scripts, and so go into <code>Inherited</code> in both <code>Script</code> and <code>Script_Extensions</code>. Others are used in just a few scripts, so are in <code>Inherited</code> in <code>Script</code>, but not in <code>Script_Extensions</code>.)</p>

<p>It is worth stressing that there are several different sets of digits in Unicode that are equivalent to 0-9 and are matchable by <code>\d</code> in a regular expression. If they are used in a single language only, they are in that language&#39;s <code>Script</code> and <code>Script_Extensions</code>. If they are used in more than one script, they will be in <code>sc=Common</code>, but only if they are used in many scripts should they be in <code>scx=Common</code>.</p>

<p>The explanation above has omitted some detail; refer to UAX#24 &quot;Unicode Script Property&quot;: <a href="https://www.unicode.org/reports/tr24">https://www.unicode.org/reports/tr24</a>.</p>

<p>A complete list of scripts and their shortcuts is in <a href="perluniprops.html">perluniprops</a>.</p>

<h3 id="Use-of-the-&quot;Is&quot;-Prefix"><a class="permalink" href="perlunicode.html#Use-of-the-%22Is%22-Prefix">#</a><a id="Use"></a><a id="Use-of-the-Is-Prefix"></a><b>Use of the <code>&quot;Is&quot;</code> Prefix</b></h3>

<p>For backward compatibility (with ancient Perl 5.6), all properties writable without using the compound form mentioned so far may have <code>Is</code> or <code>Is_</code> prepended to their name, so <code>\P{Is_Lu}</code>, for example, is equal to <code>\P{Lu}</code>, and <code>\p{IsScript:Arabic}</code> is equal to <code>\p{Arabic}</code>.</p>

<h3 id="Blocks"><a class="permalink" href="perlunicode.html#Blocks">#</a><b>Blocks</b></h3>

<p>In addition to <b>scripts</b>, Unicode also defines <b>blocks</b> of characters. The difference between scripts and blocks is that the concept of scripts is closer to natural languages, while the concept of blocks is more of an artificial grouping based on groups of Unicode characters with consecutive ordinal values. For example, the <code>&quot;Basic Latin&quot;</code> block is all the characters whose ordinals are between 0 and 127, inclusive; in other words, the ASCII characters. The <code>&quot;Latin&quot;</code> script contains some letters from this as well as several other blocks, like <code>&quot;Latin-1 Supplement&quot;</code>, <code>&quot;Latin Extended-A&quot;</code>, <i>etc.</i>, but it does not contain all the characters from those blocks. It does not, for example, contain the digits 0-9, because those digits are shared across many scripts, and hence are in the <code>Common</code> script.</p>

<p>For more about scripts versus blocks, see UAX#24 &quot;Unicode Script Property&quot;: <a href="https://www.unicode.org/reports/tr24">https://www.unicode.org/reports/tr24</a></p>

<p>The <code>Script_Extensions</code> or <code>Script</code> properties are likely to be the ones you want to use when processing natural language; the <code>Block</code> property may occasionally be useful in working with the nuts and bolts of Unicode.</p>

<p>Block names are matched in the compound form, like <code>\p{Block: Arrows}</code> or <code>\p{Blk=Hebrew}</code>. Unlike most other properties, only a few block names have a Unicode-defined short name.</p>

<p>Perl also defines single form synonyms for the block property in cases where these do not conflict with something else. But don&#39;t use any of these, because they are unstable. Since these are Perl extensions, they are subordinate to official Unicode property names; Unicode doesn&#39;t know nor care about Perl&#39;s extensions. It may happen that a name that currently means the Perl extension will later be changed without warning to mean a different Unicode property in a future version of the perl interpreter that uses a later Unicode release, and your code would no longer work. The extensions are mentioned here for completeness: Take the block name and prefix it with one of: <code>In</code> (for example <code>\p{Blk=Arrows}</code> can currently be written as <code>\p{In_Arrows}</code>); or sometimes <code>Is</code> (like <code>\p{Is_Arrows}</code>); or sometimes no prefix at all (<code>\p{Arrows}</code>). As of this writing (Unicode 9.0) there are no conflicts with using the <code>In_</code> prefix, but there are plenty with the other two forms. For example, <code>\p{Is_Hebrew}</code> and <code>\p{Hebrew}</code> mean <code>\p{Script_Extensions=Hebrew}</code> which is NOT the same thing as <code>\p{Blk=Hebrew}</code>. Our advice used to be to use the <code>In_</code> prefix as a single form way of specifying a block. But Unicode 8.0 added properties whose names begin with <code>In</code>, and it&#39;s now clear that it&#39;s only luck that&#39;s so far prevented a conflict. Using <code>In</code> is only marginally less typing than <code>Blk:</code>, and the latter&#39;s meaning is clearer anyway, and guaranteed to never conflict. So don&#39;t take chances. Use <code>\p{Blk=foo}</code> for new code. And be sure that block is what you really really want to do. In most cases scripts are what you want instead.</p>

<p>A complete list of blocks is in <a href="perluniprops.html">perluniprops</a>.</p>

<h3 id="Other-Properties"><a class="permalink" href="perlunicode.html#Other-Properties">#</a><a id="Other"></a><b>Other Properties</b></h3>

<p>There are many more properties than the very basic ones described here. A complete list is in <a href="perluniprops.html">perluniprops</a>.</p>

<p>Unicode defines all its properties in the compound form, so all single-form properties are Perl extensions. Most of these are just synonyms for the Unicode ones, but some are genuine extensions, including several that are in the compound form. And quite a few of these are actually recommended by Unicode (in <a href="https://www.unicode.org/reports/tr18">https://www.unicode.org/reports/tr18</a>).</p>

<p>This section gives some details on all extensions that aren&#39;t just synonyms for compound-form Unicode properties (for those properties, you&#39;ll have to refer to the <a href="https://www.unicode.org/reports/tr44">Unicode Standard</a>.</p>

<dl>

<dt id="\p{All}"><a class="permalink" href="perlunicode.html#%5Cp%7BAll%7D">#</a><a id="p-All"></a><b><code>\p{All}</code></b></dt>
<dd>

<p>This matches every possible code point. It is equivalent to <code>qr/./s</code>. Unlike all the other non-user-defined <code>\p{}</code> property matches, no warning is ever generated if this is property is matched against a non-Unicode code point (see <a href="perlunicode.html#Beyond-Unicode-code-points">&quot;Beyond Unicode code points&quot;</a> below).</p>

</dd>
<dt id="\p{Alnum}"><a class="permalink" href="perlunicode.html#%5Cp%7BAlnum%7D">#</a><a id="p-Alnum"></a><b><code>\p{Alnum}</code></b></dt>
<dd>

<p>This matches any <code>\p{Alphabetic}</code> or <code>\p{Decimal_Number}</code> character.</p>

</dd>
<dt id="\p{Any}"><a class="permalink" href="perlunicode.html#%5Cp%7BAny%7D">#</a><a id="p-Any"></a><b><code>\p{Any}</code></b></dt>
<dd>

<p>This matches any of the 1_114_112 Unicode code points. It is a synonym for <code>\p{Unicode}</code>.</p>

</dd>
<dt id="\p{ASCII}"><a class="permalink" href="perlunicode.html#%5Cp%7BASCII%7D">#</a><a id="p-ASCII"></a><b><code>\p{ASCII}</code></b></dt>
<dd>

<p>This matches any of the 128 characters in the US-ASCII character set, which is a subset of Unicode.</p>

</dd>
<dt id="\p{Assigned}"><a class="permalink" href="perlunicode.html#%5Cp%7BAssigned%7D">#</a><a id="p-Assigned"></a><b><code>\p{Assigned}</code></b></dt>
<dd>

<p>This matches any assigned code point; that is, any code point whose <a href="perlunicode.html#General_Category">general category</a> is not <code>Unassigned</code> (or equivalently, not <code>Cn</code>).</p>

</dd>
<dt id="\p{Blank}"><a class="permalink" href="perlunicode.html#%5Cp%7BBlank%7D">#</a><a id="p-Blank"></a><b><code>\p{Blank}</code></b></dt>
<dd>

<p>This is the same as <code>\h</code> and <code>\p{HorizSpace}</code>: A character that changes the spacing horizontally.</p>

</dd>
<dt id="\p{Decomposition_Type:-Non_Canonical}-(Short:-\p{Dt=NonCanon})"><a class="permalink" href="perlunicode.html#%5Cp%7BDecomposition_Type:-Non_Canonical%7D-(Short:-%5Cp%7BDt=NonCanon%7D)">#</a><a id="p-Decomposition_Type:-Non_Canonical-Short:-p-Dt-NonCanon"></a><b><code>\p{Decomposition_Type: Non_Canonical}</code></b> (Short: <code>\p{Dt=NonCanon}</code>)</dt>
<dd>

<p>Matches a character that has a non-canonical decomposition.</p>

<p>The <a href="perlunicode.html#Extended-Grapheme-Clusters-%28Logical-characters%29">&quot;Extended Grapheme Clusters (Logical characters)&quot;</a> section above talked about canonical decompositions. However, many more characters have a different type of decomposition, a &quot;compatible&quot; or &quot;non-canonical&quot; decomposition. The sequences that form these decompositions are not considered canonically equivalent to the pre-composed character. An example is the <code>&quot;SUPERSCRIPT ONE&quot;</code>. It is somewhat like a regular digit 1, but not exactly; its decomposition into the digit 1 is called a &quot;compatible&quot; decomposition, specifically a &quot;super&quot; decomposition. There are several such compatibility decompositions (see <a href="https://www.unicode.org/reports/tr44">https://www.unicode.org/reports/tr44</a>), including one called &quot;compat&quot;, which means some miscellaneous type of decomposition that doesn&#39;t fit into the other decomposition categories that Unicode has chosen.</p>

<p>Note that most Unicode characters don&#39;t have a decomposition, so their decomposition type is <code>&quot;None&quot;</code>.</p>

<p>For your convenience, Perl has added the <code>Non_Canonical</code> decomposition type to mean any of the several compatibility decompositions.</p>

</dd>
<dt id="\p{Graph}"><a class="permalink" href="perlunicode.html#%5Cp%7BGraph%7D">#</a><a id="p-Graph"></a><b><code>\p{Graph}</code></b></dt>
<dd>

<p>Matches any character that is graphic. Theoretically, this means a character that on a printer would cause ink to be used.</p>

</dd>
<dt id="\p{HorizSpace}"><a class="permalink" href="perlunicode.html#%5Cp%7BHorizSpace%7D">#</a><a id="p-HorizSpace"></a><b><code>\p{HorizSpace}</code></b></dt>
<dd>

<p>This is the same as <code>\h</code> and <code>\p{Blank}</code>: a character that changes the spacing horizontally.</p>

</dd>
<dt id="\p{In=*}"><a class="permalink" href="perlunicode.html#%5Cp%7BIn=*%7D">#</a><a id="p-In"></a><b><code>\p{In=*}</code></b></dt>
<dd>

<p>This is a synonym for <code>\p{Present_In=*}</code></p>

</dd>
<dt id="\p{PerlSpace}"><a class="permalink" href="perlunicode.html#%5Cp%7BPerlSpace%7D">#</a><a id="p-PerlSpace"></a><b><code>\p{PerlSpace}</code></b></dt>
<dd>

<p>This is the same as <code>\s</code>, restricted to ASCII, namely <code><span style="white-space: nowrap;">[ \f\n\r\t]</span></code> and starting in Perl v5.18, a vertical tab.</p>

<p>Mnemonic: Perl&#39;s (original) space</p>

</dd>
<dt id="\p{PerlWord}"><a class="permalink" href="perlunicode.html#%5Cp%7BPerlWord%7D">#</a><a id="p-PerlWord"></a><b><code>\p{PerlWord}</code></b></dt>
<dd>

<p>This is the same as <code>\w</code>, restricted to ASCII, namely <code>[A-Za-z0-9_]</code></p>

<p>Mnemonic: Perl&#39;s (original) word.</p>

</dd>
<dt id="\p{Posix...}"><a class="permalink" href="perlunicode.html#%5Cp%7BPosix...%7D">#</a><a id="p-Posix"></a><b><code>\p{Posix...}</code></b></dt>
<dd>

<p>There are several of these, which are equivalents, using the <code>\p{}</code> notation, for Posix classes and are described in <a href="perlrecharclass.html#POSIX-Character-Classes">&quot;POSIX Character Classes&quot; in perlrecharclass</a>.</p>

</dd>
<dt id="\p{Present_In:-*}-(Short:-\p{In=*})"><a class="permalink" href="perlunicode.html#%5Cp%7BPresent_In:-*%7D-(Short:-%5Cp%7BIn=*%7D)">#</a><a id="p-Present_In:-Short:-p-In"></a><b><code>\p{Present_In: *}</code></b> (Short: <code>\p{In=*}</code>)</dt>
<dd>

<p>This property is used when you need to know in what Unicode version(s) a character is.</p>

<p>The &quot;*&quot; above stands for some Unicode version number, such as <code>1.1</code> or <code>12.0</code>; or the &quot;*&quot; can also be <code>Unassigned</code>. This property will match the code points whose final disposition has been settled as of the Unicode release given by the version number; <code>\p{Present_In: Unassigned}</code> will match those code points whose meaning has yet to be assigned.</p>

<p>For example, <code>U+0041</code> <code>&quot;LATIN CAPITAL LETTER A&quot;</code> was present in the very first Unicode release available, which is <code>1.1</code>, so this property is true for all valid &quot;*&quot; versions. On the other hand, <code>U+1EFF</code> was not assigned until version 5.1 when it became <code>&quot;LATIN SMALL LETTER Y WITH LOOP&quot;</code>, so the only &quot;*&quot; that would match it are 5.1, 5.2, and later.</p>

<p>Unicode furnishes the <code>Age</code> property from which this is derived. The problem with Age is that a strict interpretation of it (which Perl takes) has it matching the precise release a code point&#39;s meaning is introduced in. Thus <code>U+0041</code> would match only 1.1; and <code>U+1EFF</code> only 5.1. This is not usually what you want.</p>

<p>Some non-Perl implementations of the Age property may change its meaning to be the same as the Perl <code>Present_In</code> property; just be aware of that.</p>

<p>Another confusion with both these properties is that the definition is not that the code point has been <i>assigned</i>, but that the meaning of the code point has been <i>determined</i>. This is because 66 code points will always be unassigned, and so the <code>Age</code> for them is the Unicode version in which the decision to make them so was made. For example, <code>U+FDD0</code> is to be permanently unassigned to a character, and the decision to do that was made in version 3.1, so <code>\p{Age=3.1}</code> matches this character, as also does <code>\p{Present_In: 3.1}</code> and up.</p>

</dd>
<dt id="\p{Print}"><a class="permalink" href="perlunicode.html#%5Cp%7BPrint%7D">#</a><a id="p-Print"></a><b><code>\p{Print}</code></b></dt>
<dd>

<p>This matches any character that is graphical or blank, except controls.</p>

</dd>
<dt id="\p{SpacePerl}"><a class="permalink" href="perlunicode.html#%5Cp%7BSpacePerl%7D">#</a><a id="p-SpacePerl"></a><b><code>\p{SpacePerl}</code></b></dt>
<dd>

<p>This is the same as <code>\s</code>, including beyond ASCII.</p>

<p>Mnemonic: Space, as modified by Perl. (It doesn&#39;t include the vertical tab until v5.18, which both the Posix standard and Unicode consider white space.)</p>

</dd>
<dt id="\p{Title}-and-\p{Titlecase}"><a class="permalink" href="perlunicode.html#%5Cp%7BTitle%7D-and-%5Cp%7BTitlecase%7D">#</a><a id="p-Title-and-p-Titlecase"></a><b><code>\p{Title}</code></b> and <b><code>\p{Titlecase}</code></b></dt>
<dd>

<p>Under case-sensitive matching, these both match the same code points as <code>\p{General Category=Titlecase_Letter}</code> (<code>\p{gc=lt}</code>). The difference is that under <code>/i</code> caseless matching, these match the same as <code>\p{Cased}</code>, whereas <code>\p{gc=lt}</code> matches <code>\p{Cased_Letter</code>).</p>

</dd>
<dt id="\p{Unicode}"><a class="permalink" href="perlunicode.html#%5Cp%7BUnicode%7D">#</a><a id="p-Unicode"></a><b><code>\p{Unicode}</code></b></dt>
<dd>

<p>This matches any of the 1_114_112 Unicode code points. <code>\p{Any}</code>.</p>

</dd>
<dt id="\p{VertSpace}"><a class="permalink" href="perlunicode.html#%5Cp%7BVertSpace%7D">#</a><a id="p-VertSpace"></a><b><code>\p{VertSpace}</code></b></dt>
<dd>

<p>This is the same as <code>\v</code>: A character that changes the spacing vertically.</p>

</dd>
<dt id="\p{Word}"><a class="permalink" href="perlunicode.html#%5Cp%7BWord%7D">#</a><a id="p-Word"></a><b><code>\p{Word}</code></b></dt>
<dd>

<p>This is the same as <code>\w</code>, including over 100_000 characters beyond ASCII.</p>

</dd>
<dt id="\p{XPosix...}"><a class="permalink" href="perlunicode.html#%5Cp%7BXPosix...%7D">#</a><a id="p-XPosix"></a><b><code>\p{XPosix...}</code></b></dt>
<dd>

<p>There are several of these, which are the standard Posix classes extended to the full Unicode range. They are described in <a href="perlrecharclass.html#POSIX-Character-Classes">&quot;POSIX Character Classes&quot; in perlrecharclass</a>.</p>

</dd>
</dl>

<h2 id="Comparison-of-\N{...}-and-\p{name=...}"><a class="permalink" href="perlunicode.html#Comparison-of-%5CN%7B...%7D-and-%5Cp%7Bname=...%7D">#</a><a id="Comparison"></a><a id="Comparison-of-N-...-and-p-name"></a>Comparison of <code>\N{...}</code> and <code>\p{name=...}</code></h2>

<p>Starting in Perl 5.32, you can specify a character by its name in regular expression patterns using <code>\p{name=...}</code>. This is in addition to the longstanding method of using <code>\N{...}</code>. The following summarizes the differences between these two:</p>

<pre><code class="plaintext">                      \N{...}       \p{Name=...}
can interpolate    only with eval       yes            [1]
custom names            yes             no             [2]
name aliases            yes             yes            [3]
named sequences         yes             yes            [4]
name value parsing     exact       Unicode loose       [5]</code></pre>

<dl>

<dt id="[1]"><a class="permalink" href="perlunicode.html#%5B1%5D">#</a><a id="pod-1"></a>[1]</dt>
<dd>

<p>The ability to interpolate means you can do something like</p>

<pre><code>qr/\p{na=latin capital letter $which}/</code></pre>

<p>and specify <code>$which</code> elsewhere.</p>

</dd>
<dt id="[2]"><a class="permalink" href="perlunicode.html#%5B2%5D">#</a><a id="pod-2"></a>[2]</dt>
<dd>

<p>You can create your own names for characters, and override official ones when using <code>\N{...}</code>. See <a href="charnames.html#CUSTOM-ALIASES">&quot;CUSTOM ALIASES&quot; in charnames</a>.</p>

</dd>
<dt id="[3]"><a class="permalink" href="perlunicode.html#%5B3%5D">#</a><a id="pod-3"></a>[3]</dt>
<dd>

<p>Some characters have multiple names (synonyms).</p>

</dd>
<dt id="[4]"><a class="permalink" href="perlunicode.html#%5B4%5D">#</a><a id="pod-4"></a>[4]</dt>
<dd>

<p>Some particular sequences of characters are given a single name, in addition to their individual ones.</p>

</dd>
<dt id="[5]"><a class="permalink" href="perlunicode.html#%5B5%5D">#</a><a id="pod-5"></a>[5]</dt>
<dd>

<p>Exact name value matching means you have to specify case, hyphens, underscores, and spaces precisely in the name you want. Loose matching follows the Unicode rules <a href="https://www.unicode.org/reports/tr44/tr44-24.html#UAX44-LM2">https://www.unicode.org/reports/tr44/tr44-24.html#UAX44-LM2</a>, where these are mostly irrelevant. Except for a few outlier character names, these are the same rules as are already used for any other <code>\p{...}</code> property.</p>

</dd>
</dl>

<h2 id="Wildcards-in-Property-Values"><a class="permalink" href="perlunicode.html#Wildcards-in-Property-Values">#</a><a id="Wildcards"></a>Wildcards in Property Values</h2>

<p>Starting in Perl 5.30, it is possible to do something like this:</p>

<pre><code class="plaintext">qr!\p{numeric_value=/\A[0-5]\z/}!</code></pre>

<p>or, by abbreviating and adding <code>/x</code>,</p>

<pre><code class="plaintext">qr! \p{nv= /(?x) \A [0-5] \z / }!</code></pre>

<p>This matches all code points whose numeric value is one of 0, 1, 2, 3, 4, or 5. This particular example could instead have been written as</p>

<pre><code class="plaintext">qr! \A [ \p{nv=0}\p{nv=1}\p{nv=2}\p{nv=3}\p{nv=4}\p{nv=5} ] \z !xx</code></pre>

<p>in earlier perls, so in this case this feature just makes things easier and shorter to write. If we hadn&#39;t included the <code>\A</code> and <code>\z</code>, these would have matched things like <code>1/2</code> because that contains a 1 (as well as a 2). As written, it matches things like subscripts that have these numeric values. If we only wanted the decimal digits with those numeric values, we could say,</p>

<pre><code class="plaintext">qr! (?[ \d &amp; \p{nv=/[0-5]/ ]) }!x</code></pre>

<p>The <code>\d</code> gets rid of needing to anchor the pattern, since it forces the result to only match <code>[0-9]</code>, and the <code>[0-5]</code> further restricts it.</p>

<p>The text in the above examples enclosed between the <code>&quot;/&quot;</code> characters can be just about any regular expression. It is independent of the main pattern, so doesn&#39;t share any capturing groups, <i>etc</i>. The delimiters for it must be ASCII punctuation, but it may NOT be delimited by <code>&quot;{&quot;</code>, nor <code>&quot;}&quot;</code> nor contain a literal <code>&quot;}&quot;</code>, as that delimits the end of the enclosing <code>\p{}</code>. Like any pattern, certain other delimiters are terminated by their mirror images. These are <code>&quot;(&quot;</code>, <code>&quot;[</code>&quot;, and <code>&quot;&lt;&quot;</code>. If the delimiter is any of <code>&quot;-&quot;</code>, <code>&quot;_&quot;</code>, <code>&quot;+&quot;</code>, or <code>&quot;\&quot;</code>, or is the same delimiter as is used for the enclosing pattern, it must be preceded by a backslash escape, both fore and aft.</p>

<p>Beware of using <code>&quot;$&quot;</code> to indicate to match the end of the string. It can too easily be interpreted as being a punctuation variable, like <code>$/</code>.</p>

<p>No modifiers may follow the final delimiter. Instead, use <a href="perlre.html#%28%3Fadlupimnsx-imnsx%29">&quot;(?adlupimnsx-imnsx)&quot; in perlre</a> and/or <a href="perlre.html#%28%3Fadluimnsx-imnsx%3Apattern%29">&quot;(?adluimnsx-imnsx:pattern)&quot; in perlre</a> to specify modifiers. However, certain modifiers are illegal in your wildcard subpattern. The only character set modifier specifiable is <code>/aa</code>; any other character set, and <code>-m</code>, and <code>p</code>, and <code>s</code> are all illegal. Specifying modifiers like <code>qr/.../gc</code> that aren&#39;t legal in the <code>(?...)</code> notation normally raise a warning, but with wildcard subpatterns, their use is an error. The <code>m</code> modifier is ineffective; everything that matches will be a single line.</p>

<p>By default, your pattern is matched case-insensitively, as if <code>/i</code> had been specified. You can change this by saying <code>(?-i)</code> in your pattern.</p>

<p>There are also certain operations that are illegal. You can&#39;t nest <code>\p{...}</code> and <code>\P{...}</code> calls within a wildcard subpattern, and <code>\G</code> doesn&#39;t make sense, so is also prohibited.</p>

<p>And the <code>*</code> quantifier (or its equivalent <code>(0,}</code>) is illegal.</p>

<p>This feature is not available when the left-hand side is prefixed by <code>Is_</code>, nor for any form that is marked as &quot;Discouraged&quot; in <a href="perluniprops.html#Discouraged">&quot;Discouraged&quot; in perluniprops</a>.</p>

<p>This experimental feature has been added to begin to implement <a href="https://www.unicode.org/reports/tr18/#Wildcard_Properties">https://www.unicode.org/reports/tr18/#Wildcard_Properties</a>. Using it will raise a (default-on) warning in the <code>experimental::uniprop_wildcards</code> category. We reserve the right to change its operation as we gain experience.</p>

<p>Your subpattern can be just about anything, but for it to have some utility, it should match when called with either or both of a) the full name of the property value with underscores (and/or spaces in the Block property) and some things uppercase; or b) the property value in all lowercase with spaces and underscores squeezed out. For example,</p>

<pre><code class="plaintext">qr!\p{Blk=/Old I.*/}!
qr!\p{Blk=/oldi.*/}!</code></pre>

<p>would match the same things.</p>

<p>Another example that shows that within <code>\p{...}</code>, <code>/x</code> isn&#39;t needed to have spaces:</p>

<pre><code class="plaintext">qr!\p{scx= /Hebrew|Greek/ }!</code></pre>

<p>To be safe, we should have anchored the above example, to prevent matches for something like <code>Hebrew_Braille</code>, but there aren&#39;t any script names like that, so far. A warning is issued if none of the legal values for a property are matched by your pattern. It&#39;s likely that a future release will raise a warning if your pattern ends up causing every possible code point to match.</p>

<p>Starting in 5.32, the Name, Name Aliases, and Named Sequences properties are allowed to be matched. They are considered to be a single combination property, just as has long been the case for <code>\N{}</code>. Loose matching doesn&#39;t work in exactly the same way for these as it does for the values of other properties. The rules are given in <a href="https://www.unicode.org/reports/tr44/tr44-24.html#UAX44-LM2">https://www.unicode.org/reports/tr44/tr44-24.html#UAX44-LM2</a>. As a result, Perl doesn&#39;t try loose matching for you, like it does in other properties. All letters in names are uppercase, but you can add <code>(?i)</code> to your subpattern to ignore case. If you&#39;re uncertain where a blank is, you can use <code> ?</code> in your subpattern. No character name contains an underscore, so don&#39;t bother trying to match one. The use of hyphens is particularly problematic; refer to the above link. But note that, as of Unicode 13.0, the only script in modern usage which has weirdnesses with these is Tibetan; also the two Korean characters U+116C HANGUL JUNGSEONG OE and U+1180 HANGUL JUNGSEONG O-E. Unicode makes no promises to not add hyphen-problematic names in the future.</p>

<p>Using wildcards on these is resource intensive, given the hundreds of thousands of legal names that must be checked against.</p>

<p>An example of using Name property wildcards is</p>

<pre><code class="plaintext">qr!\p{name=/(SMILING|GRINNING) FACE/}!</code></pre>

<p>Another is</p>

<pre><code class="plaintext">qr/(?[ \p{name=\/CJK\/} - \p{ideographic} ])/</code></pre>

<p>which is the 200-ish (as of Unicode 13.0) CJK characters that aren&#39;t ideographs.</p>

<p>There are certain properties that wildcard subpatterns don&#39;t currently work with. These are:</p>

<pre><code class="plaintext">Bidi Mirroring Glyph
Bidi Paired Bracket
Case Folding
Decomposition Mapping
Equivalent Unified Ideograph
Lowercase Mapping
NFKC Case Fold
Titlecase Mapping
Uppercase Mapping</code></pre>

<p>Nor is the <code>@<i>unicode_property</i>@</code> form implemented.</p>

<p>Here&#39;s a complete example of matching IPV4 internet protocol addresses in any (single) script</p>

<pre><code>no warnings &#39;experimental::regex_sets&#39;;
no warnings &#39;experimental::uniprop_wildcards&#39;;

# Can match a substring, so this intermediate regex needs to have
# context or anchoring in its final use.  Using nt=de yields decimal
# digits.  When specifying a subset of these, we must include \d to
# prevent things like U+00B2 SUPERSCRIPT TWO from matching
my $zero_through_255 =
 qr/ \b (*sr:                                  # All from same sript
           (?[ \p{nv=0} &amp; \d ])*               # Optional leading zeros
       (                                       # Then one of:
                                 \d{1,2}       #   0 - 99
           | (?[ \p{nv=1} &amp; \d ])  \d{2}       #   100 - 199
           | (?[ \p{nv=2} &amp; \d ])
              (  (?[ \p{nv=:[0-4]:} &amp; \d ]) \d #   200 - 249
               | (?[ \p{nv=5}     &amp; \d ])
                 (?[ \p{nv=:[0-5]:} &amp; \d ])    #   250 - 255
              )
       )
     )
   \b
 /x;

my $ipv4 = qr/ \A (*sr:         $zero_through_255
                        (?: [.] $zero_through_255 ) {3}
                  )
               \z
           /x;</code></pre>

<h2 id="User-Defined-Character-Properties"><a class="permalink" href="perlunicode.html#User-Defined-Character-Properties">#</a><a id="User"></a>User-Defined Character Properties</h2>

<p>You can define your own binary character properties by defining subroutines whose names begin with <code>&quot;In&quot;</code> or <code>&quot;Is&quot;</code>. (The experimental feature <a href="perlre.html#%28%3F%5B-%5D%29">&quot;(?[ ])&quot; in perlre</a> provides an alternative which allows more complex definitions.) The subroutines can be defined in any package. They override any Unicode properties expressed as the same names. The user-defined properties can be used in the regular expression <code>\p{}</code> and <code>\P{}</code> constructs; if you are using a user-defined property from a package other than the one you are in, you must specify its package in the <code>\p{}</code> or <code>\P{}</code> construct.</p>

<pre><code># assuming property IsForeign defined in Lang::
package main;  # property package name required
if ($txt =~ /\p{Lang::IsForeign}+/) { ... }

package Lang;  # property package name not required
if ($txt =~ /\p{IsForeign}+/) { ... }</code></pre>

<p>Note that the effect is compile-time and immutable once defined. However, the subroutines are passed a single parameter, which is 0 if case-sensitive matching is in effect and non-zero if caseless matching is in effect. The subroutine may return different values depending on the value of the flag, and one set of values will immutably be in effect for all case-sensitive matches, and the other set for all case-insensitive matches.</p>

<p>Note that if the regular expression is tainted, then Perl will die rather than calling the subroutine when the name of the subroutine is determined by the tainted data.</p>

<p>The subroutines must return a specially-formatted string, with one or more newline-separated lines. Each line must be one of the following:</p>

<ul>

<li><p>A single hexadecimal number denoting a code point to include.</p>

</li>
<li><p>Two hexadecimal numbers separated by horizontal whitespace (space or tabular characters) denoting a range of code points to include. The second number must not be smaller than the first.</p>

</li>
<li><p>Something to include, prefixed by <code>&quot;+&quot;</code>: a built-in character property (prefixed by <code>&quot;utf8::&quot;</code>) or a fully qualified (including package name) user-defined character property, to represent all the characters in that property; two hexadecimal code points for a range; or a single hexadecimal code point.</p>

</li>
<li><p>Something to exclude, prefixed by <code>&quot;-&quot;</code>: an existing character property (prefixed by <code>&quot;utf8::&quot;</code>) or a fully qualified (including package name) user-defined character property, to represent all the characters in that property; two hexadecimal code points for a range; or a single hexadecimal code point.</p>

</li>
<li><p>Something to negate, prefixed <code>&quot;!&quot;</code>: an existing character property (prefixed by <code>&quot;utf8::&quot;</code>) or a fully qualified (including package name) user-defined character property, to represent all the characters in that property; two hexadecimal code points for a range; or a single hexadecimal code point.</p>

</li>
<li><p>Something to intersect with, prefixed by <code>&quot;&amp;&quot;</code>: an existing character property (prefixed by <code>&quot;utf8::&quot;</code>) or a fully qualified (including package name) user-defined character property, for all the characters except the characters in the property; two hexadecimal code points for a range; or a single hexadecimal code point.</p>

</li>
</ul>

<p>For example, to define a property that covers both the Japanese syllabaries (hiragana and katakana), you can define</p>

<pre><code>sub InKana {
    return &lt;&lt;END;
3040\t309F
30A0\t30FF
END
}</code></pre>

<p>Imagine that the here-doc end marker is at the beginning of the line. Now you can use <code>\p{InKana}</code> and <code>\P{InKana}</code>.</p>

<p>You could also have used the existing block property names:</p>

<pre><code>sub InKana {
    return &lt;&lt;&#39;END&#39;;
+utf8::InHiragana
+utf8::InKatakana
END
}</code></pre>

<p>Suppose you wanted to match only the allocated characters, not the raw block ranges: in other words, you want to remove the unassigned characters:</p>

<pre><code>sub InKana {
    return &lt;&lt;&#39;END&#39;;
+utf8::InHiragana
+utf8::InKatakana
-utf8::IsCn
END
}</code></pre>

<p>The negation is useful for defining (surprise!) negated classes.</p>

<pre><code>sub InNotKana {
    return &lt;&lt;&#39;END&#39;;
!utf8::InHiragana
-utf8::InKatakana
+utf8::IsCn
END
}</code></pre>

<p>This will match all non-Unicode code points, since every one of them is not in Kana. You can use intersection to exclude these, if desired, as this modified example shows:</p>

<pre><code>sub InNotKana {
    return &lt;&lt;&#39;END&#39;;
!utf8::InHiragana
-utf8::InKatakana
+utf8::IsCn
&amp;utf8::Any
END
}</code></pre>

<p><code>&amp;utf8::Any</code> must be the last line in the definition.</p>

<p>Intersection is used generally for getting the common characters matched by two (or more) classes. It&#39;s important to remember not to use <code>&quot;&amp;&quot;</code> for the first set; that would be intersecting with nothing, resulting in an empty set. (Similarly using <code>&quot;-&quot;</code> for the first set does nothing).</p>

<p>Unlike non-user-defined <code>\p{}</code> property matches, no warning is ever generated if these properties are matched against a non-Unicode code point (see <a href="perlunicode.html#Beyond-Unicode-code-points">&quot;Beyond Unicode code points&quot;</a> below).</p>

<h2 id="User-Defined-Case-Mappings-(for-serious-hackers-only)"><a class="permalink" href="perlunicode.html#User-Defined-Case-Mappings-(for-serious-hackers-only)">#</a><a id="User1"></a><a id="User-Defined-Case-Mappings-for-serious-hackers-only"></a>User-Defined Case Mappings (for serious hackers only)</h2>

<p><b>This feature has been removed as of Perl 5.16.</b> The CPAN module <code><a href="./Unicode::Casing.html">Unicode::Casing</a></code> provides better functionality without the drawbacks that this feature had. If you are using a Perl earlier than 5.16, this feature was most fully documented in the 5.14 version of this pod: <a href="http://perldoc.perl.org/5.14.0/perlunicode.html#User-Defined-Case-Mappings-%28for-serious-hackers-only%29">http://perldoc.perl.org/5.14.0/perlunicode.html#User-Defined-Case-Mappings-%28for-serious-hackers-only%29</a></p>

<h2 id="Character-Encodings-for-Input-and-Output"><a class="permalink" href="perlunicode.html#Character-Encodings-for-Input-and-Output">#</a><a id="Character"></a>Character Encodings for Input and Output</h2>

<p>See <a href="Encode.html">Encode</a>.</p>

<h2 id="Unicode-Regular-Expression-Support-Level"><a class="permalink" href="perlunicode.html#Unicode-Regular-Expression-Support-Level">#</a><a id="Unicode1"></a>Unicode Regular Expression Support Level</h2>

<p>The following list of Unicode supported features for regular expressions describes all features currently directly supported by core Perl. The references to &quot;Level <i>N</i>&quot; and the section numbers refer to <a href="https://www.unicode.org/reports/tr18">UTS#18 &quot;Unicode Regular Expressions&quot;</a>, version 18, October 2016.</p>

<h3 id="Level-1-Basic-Unicode-Support"><a class="permalink" href="perlunicode.html#Level-1-Basic-Unicode-Support">#</a><a id="Level"></a><a id="Level-1---Basic-Unicode-Support"></a>Level 1 - Basic Unicode Support</h3>

<pre><code class="plaintext">RL1.1   Hex Notation                     - Done          [1]
RL1.2   Properties                       - Done          [2]
RL1.2a  Compatibility Properties         - Done          [3]
RL1.3   Subtraction and Intersection     - Experimental  [4]
RL1.4   Simple Word Boundaries           - Done          [5]
RL1.5   Simple Loose Matches             - Done          [6]
RL1.6   Line Boundaries                  - Partial       [7]
RL1.7   Supplementary Code Points        - Done          [8]</code></pre>

<dl>

<dt id="[1]-\N{U+...}-and-\x{...}"><a class="permalink" href="perlunicode.html#%5B1%5D-%5CN%7BU+...%7D-and-%5Cx%7B...%7D">#</a><a id="N-U-...-and-x"></a>[1] <code>\N{U+...}</code> and <code>\x{...}</code></dt>
<dd>

</dd>
<dt id="[2]-\p{...}-\P{...}.-This-requirement-is-for-a-minimal-list-of-properties.-Perl-supports-these.-See-R2.7-for-other-properties."><a class="permalink" href="perlunicode.html#%5B2%5D-%5Cp%7B...%7D-%5CP%7B...%7D.-This-requirement-is-for-a-minimal-list-of-properties.-Perl-supports-these.-See-R2.7-for-other-properties.">#</a><a id="p-...-P-...-.-This-requirement-is-for-a-minimal-list-of-properties.-Perl-supports-these.-See-R2.7-for-other-properties"></a>[2] <code>\p{...}</code> <code>\P{...}</code>. This requirement is for a minimal list of properties. Perl supports these. See R2.7 for other properties.</dt>
<dd>

</dd>
<dt id="[3]-Perl-has-\d-\D-\s-\S-\w-\W-\X-[:prop:]-[:^prop:],-plus-all-the-properties-specified-by-https://www.unicode.org/reports/tr18/#Compatibility_Properties.-These-are-described-above-in-&quot;Other-Properties&quot;"><a class="permalink" href="perlunicode.html#%5B3%5D-Perl-has-%5Cd-%5CD-%5Cs-%5CS-%5Cw-%5CW-%5CX-%5B:prop:%5D-%5B:%5Eprop:%5D,-plus-all-the-properties-specified-by-https://www.unicode.org/reports/tr18/%23Compatibility_Properties.-These-are-described-above-in-%22Other-Properties%22">#</a><a id="Perl-has-d-D-s-S-w-W-X-:prop:-:-prop:-plus-all-the-properties-specified-by-https:-www.unicode.org-reports-tr18-Compatibility_Properties.-These-are-described-above-in-Other-Properties"></a>[3] Perl has <code>\d</code> <code>\D</code> <code>\s</code> <code>\S</code> <code>\w</code> <code>\W</code> <code>\X</code> <code>[:<i>prop</i>:]</code> <code>[:^<i>prop</i>:]</code>, plus all the properties specified by <a href="https://www.unicode.org/reports/tr18/#Compatibility_Properties">https://www.unicode.org/reports/tr18/#Compatibility_Properties</a>. These are described above in <a href="perlunicode.html#Other-Properties">&quot;Other Properties&quot;</a></dt>
<dd>

</dd>
<dt id="[4]1"><a class="permalink" href="perlunicode.html#%5B4%5D1">#</a><a id="pod-41"></a>[4]</dt>
<dd>

<p>The experimental feature <code>&quot;(?[...])&quot;</code> starting in v5.18 accomplishes this.</p>

<p>See <a href="perlre.html#%28%3F%5B-%5D%29">&quot;(?[ ])&quot; in perlre</a>. If you don&#39;t want to use an experimental feature, you can use one of the following:</p>

<ul>

<li><p>Regular expression lookahead</p>

<p>You can mimic class subtraction using lookahead. For example, what UTS#18 might write as</p>

<pre><code class="plaintext">[{Block=Greek}-[{UNASSIGNED}]]</code></pre>

<p>in Perl can be written as:</p>

<pre><code class="plaintext">(?!\p{Unassigned})\p{Block=Greek}
(?=\p{Assigned})\p{Block=Greek}</code></pre>

<p>But in this particular example, you probably really want</p>

<pre><code class="plaintext">\p{Greek}</code></pre>

<p>which will match assigned characters known to be part of the Greek script.</p>

</li>
<li><p>CPAN module <code><a href="./Unicode::Regex::Set.html">Unicode::Regex::Set</a></code></p>

<p>It does implement the full UTS#18 grouping, intersection, union, and removal (subtraction) syntax.</p>

</li>
<li><p><a href="perlunicode.html#User-Defined-Character-Properties">&quot;User-Defined Character Properties&quot;</a></p>

<p><code>&quot;+&quot;</code> for union, <code>&quot;-&quot;</code> for removal (set-difference), <code>&quot;&amp;&quot;</code> for intersection</p>

</li>
</ul>

</dd>
<dt id="[5]-\b-\B-meet-most,-but-not-all,-the-details-of-this-requirement,-but-\b{wb}-and-\B{wb}-do,-as-well-as-the-stricter-R2.3."><a class="permalink" href="perlunicode.html#%5B5%5D-%5Cb-%5CB-meet-most,-but-not-all,-the-details-of-this-requirement,-but-%5Cb%7Bwb%7D-and-%5CB%7Bwb%7D-do,-as-well-as-the-stricter-R2.3.">#</a><a id="b-B-meet-most-but-not-all-the-details-of-this-requirement-but-b-wb-and-B-wb-do-as-well-as-the-stricter-R2.3"></a>[5] <code>\b</code> <code>\B</code> meet most, but not all, the details of this requirement, but <code>\b{wb}</code> and <code>\B{wb}</code> do, as well as the stricter R2.3.</dt>
<dd>

</dd>
<dt id="[6]"><a class="permalink" href="perlunicode.html#%5B6%5D">#</a><a id="pod-6"></a>[6]</dt>
<dd>

<p>Note that Perl does Full case-folding in matching, not Simple:</p>

<p>For example <code>U+1F88</code> is equivalent to <code>U+1F00 U+03B9</code>, instead of just <code>U+1F80</code>. This difference matters mainly for certain Greek capital letters with certain modifiers: the Full case-folding decomposes the letter, while the Simple case-folding would map it to a single character.</p>

</dd>
<dt id="[7]"><a class="permalink" href="perlunicode.html#%5B7%5D">#</a><a id="pod-7"></a>[7]</dt>
<dd>

<p>The reason this is considered to be only partially implemented is that Perl has <a href="perlrebackslash.html#%5Cb%7Blb%7D"><code>qr/\b{lb}/</code></a> and <code><a href="./Unicode::LineBreak.html">Unicode::LineBreak</a></code> that are conformant with <a href="https://www.unicode.org/reports/tr14">UAX#14 &quot;Unicode Line Breaking Algorithm&quot;</a>. The regular expression construct provides default behavior, while the heavier-weight module provides customizable line breaking.</p>

<p>But Perl treats <code>\n</code> as the start- and end-line delimiter, whereas Unicode specifies more characters that should be so-interpreted.</p>

<p>These are:</p>

<pre><code class="plaintext">VT   U+000B  (\v in C)
FF   U+000C  (\f)
CR   U+000D  (\r)
NEL  U+0085
LS   U+2028
PS   U+2029</code></pre>

<p><code>^</code> and <code>$</code> in regular expression patterns are supposed to match all these, but don&#39;t. These characters also don&#39;t, but should, affect <code>&lt;&gt;</code> <code>$.</code>, and script line numbers.</p>

<p>Also, lines should not be split within <code>CRLF</code> (i.e. there is no empty line between <code>\r</code> and <code>\n</code>). For <code>CRLF</code>, try the <code>:crlf</code> layer (see <a href="PerlIO.html">PerlIO</a>).</p>

</dd>
<dt id="[8]-UTF-8/UTF-EBDDIC-used-in-Perl-allows-not-only-U+10000-to-U+10FFFF-but-also-beyond-U+10FFFF"><a class="permalink" href="perlunicode.html#%5B8%5D-UTF-8/UTF-EBDDIC-used-in-Perl-allows-not-only-U+10000-to-U+10FFFF-but-also-beyond-U+10FFFF">#</a><a id="UTF-8-UTF-EBDDIC-used-in-Perl-allows-not-only-U-10000-to-U-10FFFF-but-also-beyond-U-10FFFF"></a>[8] UTF-8/UTF-EBDDIC used in Perl allows not only <code>U+10000</code> to <code>U+10FFFF</code> but also beyond <code>U+10FFFF</code></dt>
<dd>

</dd>
</dl>

<h3 id="Level-2-Extended-Unicode-Support"><a class="permalink" href="perlunicode.html#Level-2-Extended-Unicode-Support">#</a><a id="Level1"></a><a id="Level-2---Extended-Unicode-Support"></a>Level 2 - Extended Unicode Support</h3>

<pre><code class="plaintext">RL2.1   Canonical Equivalents           - Retracted     [9]
                                          by Unicode
RL2.2   Extended Grapheme Clusters and  - Partial       [10]
        Character Classes with Strings
RL2.3   Default Word Boundaries         - Done          [11]
RL2.4   Default Case Conversion         - Done
RL2.5   Name Properties                 - Done
RL2.6   Wildcards in Property Values    - Partial       [12]
RL2.7   Full Properties                 - Partial       [13]
RL2.8   Optional Properties             - Partial       [14]</code></pre>

<dl>

<dt id="[9]-Unicode-has-rewritten-this-portion-of-UTS#18-to-say-that-getting-canonical-equivalence-(see-UAX#15-&quot;Unicode-Normalization-Forms&quot;)-is-basically-to-be-done-at-the-programmer-level.-Use-NFD-to-write-both-your-regular-expressions-and-text-to-match-them-against-(you-can-use-Unicode::Normalize)."><a class="permalink" href="perlunicode.html#%5B9%5D-Unicode-has-rewritten-this-portion-of-UTS%2318-to-say-that-getting-canonical-equivalence-(see-UAX%2315-%22Unicode-Normalization-Forms%22)-is-basically-to-be-done-at-the-programmer-level.-Use-NFD-to-write-both-your-regular-expressions-and-text-to-match-them-against-(you-can-use-Unicode::Normalize).">#</a><a id="Unicode-has-rewritten-this-portion-of-UTS-18-to-say-that-getting-canonical-equivalence-see-UAX-15-Unicode-Normalization-Forms-is-basically-to-be-done-at-the-programmer-level.-Use-NFD-to-write-both-your-regular-expressions-and-text-to-match-them-against-you-can-use-Unicode::Normalize"></a>[9] Unicode has rewritten this portion of UTS#18 to say that getting canonical equivalence (see UAX#15 <a href="https://www.unicode.org/reports/tr15">&quot;Unicode Normalization Forms&quot;</a>) is basically to be done at the programmer level. Use NFD to write both your regular expressions and text to match them against (you can use <a href="./Unicode::Normalize.html">Unicode::Normalize</a>).</dt>
<dd>

</dd>
<dt id="[10]-Perl-has-\X-and-\b{gcb}.-Unicode-has-retracted-their-&quot;Grapheme-Cluster-Mode&quot;,-and-recently-added-string-properties,-which-Perl-does-not-yet-support."><a class="permalink" href="perlunicode.html#%5B10%5D-Perl-has-%5CX-and-%5Cb%7Bgcb%7D.-Unicode-has-retracted-their-%22Grapheme-Cluster-Mode%22,-and-recently-added-string-properties,-which-Perl-does-not-yet-support.">#</a><a id="Perl-has-X-and-b-gcb-.-Unicode-has-retracted-their-Grapheme-Cluster-Mode-and-recently-added-string-properties-which-Perl-does-not-yet-support"></a>[10] Perl has <code>\X</code> and <code>\b{gcb}</code>. Unicode has retracted their &quot;Grapheme Cluster Mode&quot;, and recently added string properties, which Perl does not yet support.</dt>
<dd>

</dd>
<dt id="[11]-see-UAX#29-&quot;Unicode-Text-Segmentation&quot;,"><a class="permalink" href="perlunicode.html#%5B11%5D-see-UAX%2329-%22Unicode-Text-Segmentation%22,">#</a><a id="see-UAX-29-Unicode-Text-Segmentation"></a>[11] see <a href="https://www.unicode.org/reports/tr29">UAX#29 &quot;Unicode Text Segmentation&quot;</a>,</dt>
<dd>

</dd>
<dt id="[12]-see-&quot;Wildcards-in-Property-Values&quot;-above."><a class="permalink" href="perlunicode.html#%5B12%5D-see-%22Wildcards-in-Property-Values%22-above.">#</a><a id="see-Wildcards-in-Property-Values-above"></a>[12] see <a href="perlunicode.html#Wildcards-in-Property-Values">&quot;Wildcards in Property Values&quot;</a> above.</dt>
<dd>

</dd>
<dt id="[13]-Perl-supports-all-the-properties-in-the-Unicode-Character-Database-(UCD).-It-does-not-yet-support-the-listed-properties-that-come-from-other-Unicode-sources."><a class="permalink" href="perlunicode.html#%5B13%5D-Perl-supports-all-the-properties-in-the-Unicode-Character-Database-(UCD).-It-does-not-yet-support-the-listed-properties-that-come-from-other-Unicode-sources.">#</a><a id="Perl-supports-all-the-properties-in-the-Unicode-Character-Database-UCD-.-It-does-not-yet-support-the-listed-properties-that-come-from-other-Unicode-sources"></a>[13] Perl supports all the properties in the Unicode Character Database (UCD). It does not yet support the listed properties that come from other Unicode sources.</dt>
<dd>

</dd>
<dt id="[14]-The-only-optional-property-that-Perl-supports-is-Named-Sequence.-None-of-these-properties-are-in-the-UCD."><a class="permalink" href="perlunicode.html#%5B14%5D-The-only-optional-property-that-Perl-supports-is-Named-Sequence.-None-of-these-properties-are-in-the-UCD.">#</a><a id="The-only-optional-property-that-Perl-supports-is-Named-Sequence.-None-of-these-properties-are-in-the-UCD"></a>[14] The only optional property that Perl supports is Named Sequence. None of these properties are in the UCD.</dt>
<dd>

</dd>
</dl>

<h3 id="Level-3-Tailored-Support"><a class="permalink" href="perlunicode.html#Level-3-Tailored-Support">#</a><a id="Level2"></a><a id="Level-3---Tailored-Support"></a>Level 3 - Tailored Support</h3>

<p>This has been retracted by Unicode.</p>

<h2 id="Unicode-Encodings"><a class="permalink" href="perlunicode.html#Unicode-Encodings">#</a><a id="Unicode2"></a>Unicode Encodings</h2>

<p>Unicode characters are assigned to <i>code points</i>, which are abstract numbers. To use these numbers, various encodings are needed.</p>

<ul>

<li><p>UTF-8</p>

<p>UTF-8 is a variable-length (1 to 4 bytes), byte-order independent encoding. In most of Perl&#39;s documentation, including elsewhere in this document, the term &quot;UTF-8&quot; means also &quot;UTF-EBCDIC&quot;. But in this section, &quot;UTF-8&quot; refers only to the encoding used on ASCII platforms. It is a superset of 7-bit US-ASCII, so anything encoded in ASCII has the identical representation when encoded in UTF-8.</p>

<p>The following table is from Unicode 3.2.</p>

<pre><code class="plaintext">Code Points            1st Byte  2nd Byte  3rd Byte 4th Byte

  U+0000..U+007F       00..7F
  U+0080..U+07FF     * C2..DF    80..BF
  U+0800..U+0FFF       E0      * A0..BF    80..BF
  U+1000..U+CFFF       E1..EC    80..BF    80..BF
  U+D000..U+D7FF       ED        80..9F    80..BF
  U+D800..U+DFFF       +++++ utf16 surrogates, not legal utf8 +++++
  U+E000..U+FFFF       EE..EF    80..BF    80..BF
 U+10000..U+3FFFF      F0      * 90..BF    80..BF    80..BF
 U+40000..U+FFFFF      F1..F3    80..BF    80..BF    80..BF
U+100000..U+10FFFF     F4        80..8F    80..BF    80..BF</code></pre>

<p>Note the gaps marked by &quot;*&quot; before several of the byte entries above. These are caused by legal UTF-8 avoiding non-shortest encodings: it is technically possible to UTF-8-encode a single code point in different ways, but that is explicitly forbidden, and the shortest possible encoding should always be used (and that is what Perl does).</p>

<p>Another way to look at it is via bits:</p>

<pre><code class="plaintext">               Code Points  1st Byte  2nd Byte  3rd Byte  4th Byte

                  0aaaaaaa  0aaaaaaa
          00000bbbbbaaaaaa  110bbbbb  10aaaaaa
          ccccbbbbbbaaaaaa  1110cccc  10bbbbbb  10aaaaaa
00000dddccccccbbbbbbaaaaaa  11110ddd  10cccccc  10bbbbbb  10aaaaaa</code></pre>

<p>As you can see, the continuation bytes all begin with <code>&quot;10&quot;</code>, and the leading bits of the start byte tell how many bytes there are in the encoded character.</p>

<p>The original UTF-8 specification allowed up to 6 bytes, to allow encoding of numbers up to <code>0x7FFF_FFFF</code>. Perl continues to allow those, and has extended that up to 13 bytes to encode code points up to what can fit in a 64-bit word. However, Perl will warn if you output any of these as being non-portable; and under strict UTF-8 input protocols, they are forbidden. In addition, it is now illegal to use a code point larger than what a signed integer variable on your system can hold. On 32-bit ASCII systems, this means <code>0x7FFF_FFFF</code> is the legal maximum (much higher on 64-bit systems).</p>

</li>
<li><p>UTF-EBCDIC</p>

<p>Like UTF-8, but EBCDIC-safe, in the way that UTF-8 is ASCII-safe. This means that all the basic characters (which includes all those that have ASCII equivalents (like <code>&quot;A&quot;</code>, <code>&quot;0&quot;</code>, <code>&quot;%&quot;</code>, <i>etc.</i>) are the same in both EBCDIC and UTF-EBCDIC.)</p>

<p>UTF-EBCDIC is used on EBCDIC platforms. It generally requires more bytes to represent a given code point than UTF-8 does; the largest Unicode code points take 5 bytes to represent (instead of 4 in UTF-8), and, extended for 64-bit words, it uses 14 bytes instead of 13 bytes in UTF-8.</p>

</li>
<li><p>UTF-16, UTF-16BE, UTF-16LE, Surrogates, and <code>BOM</code>&#39;s (Byte Order Marks)</p>

<p>The followings items are mostly for reference and general Unicode knowledge, Perl doesn&#39;t use these constructs internally.</p>

<p>Like UTF-8, UTF-16 is a variable-width encoding, but where UTF-8 uses 8-bit code units, UTF-16 uses 16-bit code units. All code points occupy either 2 or 4 bytes in UTF-16: code points <code>U+0000..U+FFFF</code> are stored in a single 16-bit unit, and code points <code>U+10000..U+10FFFF</code> in two 16-bit units. The latter case is using <i>surrogates</i>, the first 16-bit unit being the <i>high surrogate</i>, and the second being the <i>low surrogate</i>.</p>

<p>Surrogates are code points set aside to encode the <code>U+10000..U+10FFFF</code> range of Unicode code points in pairs of 16-bit units. The <i>high surrogates</i> are the range <code>U+D800..U+DBFF</code> and the <i>low surrogates</i> are the range <code>U+DC00..U+DFFF</code>. The surrogate encoding is</p>

<pre><code>$hi = ($uni - 0x10000) / 0x400 + 0xD800;
$lo = ($uni - 0x10000) % 0x400 + 0xDC00;</code></pre>

<p>and the decoding is</p>

<pre><code>$uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00);</code></pre>

<p>Because of the 16-bitness, UTF-16 is byte-order dependent. UTF-16 itself can be used for in-memory computations, but if storage or transfer is required either UTF-16BE (big-endian) or UTF-16LE (little-endian) encodings must be chosen.</p>

<p>This introduces another problem: what if you just know that your data is UTF-16, but you don&#39;t know which endianness? Byte Order Marks, or <code>BOM</code>&#39;s, are a solution to this. A special character has been reserved in Unicode to function as a byte order marker: the character with the code point <code>U+FEFF</code> is the <code>BOM</code>.</p>

<p>The trick is that if you read a <code>BOM</code>, you will know the byte order, since if it was written on a big-endian platform, you will read the bytes <code>0xFE 0xFF</code>, but if it was written on a little-endian platform, you will read the bytes <code>0xFF 0xFE</code>. (And if the originating platform was writing in ASCII platform UTF-8, you will read the bytes <code>0xEF 0xBB 0xBF</code>.)</p>

<p>The way this trick works is that the character with the code point <code>U+FFFE</code> is not supposed to be in input streams, so the sequence of bytes <code>0xFF 0xFE</code> is unambiguously &quot;<code>BOM</code>, represented in little-endian format&quot; and cannot be <code>U+FFFE</code>, represented in big-endian format&quot;.</p>

<p>Surrogates have no meaning in Unicode outside their use in pairs to represent other code points. However, Perl allows them to be represented individually internally, for example by saying <code>chr(0xD801)</code>, so that all code points, not just those valid for open interchange, are representable. Unicode does define semantics for them, such as their <code><a href="perlunicode.html#General_Category">&quot;General_Category&quot;</a></code> is <code>&quot;Cs&quot;</code>. But because their use is somewhat dangerous, Perl will warn (using the warning category <code>&quot;surrogate&quot;</code>, which is a sub-category of <code>&quot;utf8&quot;</code>) if an attempt is made to do things like take the lower case of one, or match case-insensitively, or to output them. (But don&#39;t try this on Perls before 5.14.)</p>

</li>
<li><p>UTF-32, UTF-32BE, UTF-32LE</p>

<p>The UTF-32 family is pretty much like the UTF-16 family, except that the units are 32-bit, and therefore the surrogate scheme is not needed. UTF-32 is a fixed-width encoding. The <code>BOM</code> signatures are <code>0x00 0x00 0xFE 0xFF</code> for BE and <code>0xFF 0xFE 0x00 0x00</code> for LE.</p>

</li>
<li><p>UCS-2, UCS-4</p>

<p>Legacy, fixed-width encodings defined by the ISO 10646 standard. UCS-2 is a 16-bit encoding. Unlike UTF-16, UCS-2 is not extensible beyond <code>U+FFFF</code>, because it does not use surrogates. UCS-4 is a 32-bit encoding, functionally identical to UTF-32 (the difference being that UCS-4 forbids neither surrogates nor code points larger than <code>0x10_FFFF</code>).</p>

</li>
<li><p>UTF-7</p>

<p>A seven-bit safe (non-eight-bit) encoding, which is useful if the transport or storage is not eight-bit safe. Defined by RFC 2152.</p>

</li>
</ul>

<h2 id="Noncharacter-code-points"><a class="permalink" href="perlunicode.html#Noncharacter-code-points">#</a><a id="Noncharacter"></a>Noncharacter code points</h2>

<p>66 code points are set aside in Unicode as &quot;noncharacter code points&quot;. These all have the <code>Unassigned</code> (<code>Cn</code>) <code><a href="perlunicode.html#General_Category">&quot;General_Category&quot;</a></code>, and no character will ever be assigned to any of them. They are the 32 code points between <code>U+FDD0</code> and <code>U+FDEF</code> inclusive, and the 34 code points:</p>

<pre><code class="plaintext">U+FFFE   U+FFFF
U+1FFFE  U+1FFFF
U+2FFFE  U+2FFFF
...
U+EFFFE  U+EFFFF
U+FFFFE  U+FFFFF
U+10FFFE U+10FFFF</code></pre>

<p>Until Unicode 7.0, the noncharacters were &quot;<b>forbidden</b> for use in open interchange of Unicode text data&quot;, so that code that processed those streams could use these code points as sentinels that could be mixed in with character data, and would always be distinguishable from that data. (Emphasis above and in the next paragraph are added in this document.)</p>

<p>Unicode 7.0 changed the wording so that they are &quot;<b>not recommended</b> for use in open interchange of Unicode text data&quot;. The 7.0 Standard goes on to say:</p>

<ul>

<p>&quot;If a noncharacter is received in open interchange, an application is not required to interpret it in any way. It is good practice, however, to recognize it as a noncharacter and to take appropriate action, such as replacing it with <code>U+FFFD</code> replacement character, to indicate the problem in the text. It is not recommended to simply delete noncharacter code points from such text, because of the potential security issues caused by deleting uninterpreted characters. (See conformance clause C7 in Section 3.2, Conformance Requirements, and <a href="https://www.unicode.org/reports/tr36/#Substituting_for_Ill_Formed_Subsequences">Unicode Technical Report #36, &quot;Unicode Security Considerations&quot;</a>).&quot;</p>

</ul>

<p>This change was made because it was found that various commercial tools like editors, or for things like source code control, had been written so that they would not handle program files that used these code points, effectively precluding their use almost entirely! And that was never the intent. They&#39;ve always been meant to be usable within an application, or cooperating set of applications, at will.</p>

<p>If you&#39;re writing code, such as an editor, that is supposed to be able to handle any Unicode text data, then you shouldn&#39;t be using these code points yourself, and instead allow them in the input. If you need sentinels, they should instead be something that isn&#39;t legal Unicode. For UTF-8 data, you can use the bytes 0xC1 and 0xC2 as sentinels, as they never appear in well-formed UTF-8. (There are equivalents for UTF-EBCDIC). You can also store your Unicode code points in integer variables and use negative values as sentinels.</p>

<p>If you&#39;re not writing such a tool, then whether you accept noncharacters as input is up to you (though the Standard recommends that you not). If you do strict input stream checking with Perl, these code points continue to be forbidden. This is to maintain backward compatibility (otherwise potential security holes could open up, as an unsuspecting application that was written assuming the noncharacters would be filtered out before getting to it, could now, without warning, start getting them). To do strict checking, you can use the layer <code>:encoding(&#39;UTF-8&#39;)</code>.</p>

<p>Perl continues to warn (using the warning category <code>&quot;nonchar&quot;</code>, which is a sub-category of <code>&quot;utf8&quot;</code>) if an attempt is made to output noncharacters.</p>

<h2 id="Beyond-Unicode-code-points"><a class="permalink" href="perlunicode.html#Beyond-Unicode-code-points">#</a><a id="Beyond"></a>Beyond Unicode code points</h2>

<p>The maximum Unicode code point is <code>U+10FFFF</code>, and Unicode only defines operations on code points up through that. But Perl works on code points up to the maximum permissible signed number available on the platform. However, Perl will not accept these from input streams unless lax rules are being used, and will warn (using the warning category <code>&quot;non_unicode&quot;</code>, which is a sub-category of <code>&quot;utf8&quot;</code>) if any are output.</p>

<p>Since Unicode rules are not defined on these code points, if a Unicode-defined operation is done on them, Perl uses what we believe are sensible rules, while generally warning, using the <code>&quot;non_unicode&quot;</code> category. For example, <code>uc(&quot;\x{11_0000}&quot;)</code> will generate such a warning, returning the input parameter as its result, since Perl defines the uppercase of every non-Unicode code point to be the code point itself. (All the case changing operations, not just uppercasing, work this way.)</p>

<p>The situation with matching Unicode properties in regular expressions, the <code>\p{}</code> and <code>\P{}</code> constructs, against these code points is not as clear cut, and how these are handled has changed as we&#39;ve gained experience.</p>

<p>One possibility is to treat any match against these code points as undefined. But since Perl doesn&#39;t have the concept of a match being undefined, it converts this to failing or <code>FALSE</code>. This is almost, but not quite, what Perl did from v5.14 (when use of these code points became generally reliable) through v5.18. The difference is that Perl treated all <code>\p{}</code> matches as failing, but all <code>\P{}</code> matches as succeeding.</p>

<p>One problem with this is that it leads to unexpected, and confusing results in some cases:</p>

<pre><code class="plaintext">chr(0x110000) =~ \p{ASCII_Hex_Digit=True}      # Failed on &lt;= v5.18
chr(0x110000) =~ \p{ASCII_Hex_Digit=False}     # Failed! on &lt;= v5.18</code></pre>

<p>That is, it treated both matches as undefined, and converted that to false (raising a warning on each). The first case is the expected result, but the second is likely counterintuitive: &quot;How could both be false when they are complements?&quot; Another problem was that the implementation optimized many Unicode property matches down to already existing simpler, faster operations, which don&#39;t raise the warning. We chose to not forgo those optimizations, which help the vast majority of matches, just to generate a warning for the unlikely event that an above-Unicode code point is being matched against.</p>

<p>As a result of these problems, starting in v5.20, what Perl does is to treat non-Unicode code points as just typical unassigned Unicode characters, and matches accordingly. (Note: Unicode has atypical unassigned code points. For example, it has noncharacter code points, and ones that, when they do get assigned, are destined to be written Right-to-left, as Arabic and Hebrew are. Perl assumes that no non-Unicode code point has any atypical properties.)</p>

<p>Perl, in most cases, will raise a warning when matching an above-Unicode code point against a Unicode property when the result is <code>TRUE</code> for <code>\p{}</code>, and <code>FALSE</code> for <code>\P{}</code>. For example:</p>

<pre><code class="plaintext">chr(0x110000) =~ \p{ASCII_Hex_Digit=True}      # Fails, no warning
chr(0x110000) =~ \p{ASCII_Hex_Digit=False}     # Succeeds, with warning</code></pre>

<p>In both these examples, the character being matched is non-Unicode, so Unicode doesn&#39;t define how it should match. It clearly isn&#39;t an ASCII hex digit, so the first example clearly should fail, and so it does, with no warning. But it is arguable that the second example should have an undefined, hence <code>FALSE</code>, result. So a warning is raised for it.</p>

<p>Thus the warning is raised for many fewer cases than in earlier Perls, and only when what the result is could be arguable. It turns out that none of the optimizations made by Perl (or are ever likely to be made) cause the warning to be skipped, so it solves both problems of Perl&#39;s earlier approach. The most commonly used property that is affected by this change is <code>\p{Unassigned}</code> which is a short form for <code>\p{General_Category=Unassigned}</code>. Starting in v5.20, all non-Unicode code points are considered <code>Unassigned</code>. In earlier releases the matches failed because the result was considered undefined.</p>

<p>The only place where the warning is not raised when it might ought to have been is if optimizations cause the whole pattern match to not even be attempted. For example, Perl may figure out that for a string to match a certain regular expression pattern, the string has to contain the substring <code>&quot;foobar&quot;</code>. Before attempting the match, Perl may look for that substring, and if not found, immediately fail the match without actually trying it; so no warning gets generated even if the string contains an above-Unicode code point.</p>

<p>This behavior is more &quot;Do what I mean&quot; than in earlier Perls for most applications. But it catches fewer issues for code that needs to be strictly Unicode compliant. Therefore there is an additional mode of operation available to accommodate such code. This mode is enabled if a regular expression pattern is compiled within the lexical scope where the <code>&quot;non_unicode&quot;</code> warning class has been made fatal, say by:</p>

<pre><code class="plaintext">use warnings FATAL =&gt; &quot;non_unicode&quot;</code></pre>

<p>(see <a href="warnings.html">warnings</a>). In this mode of operation, Perl will raise the warning for all matches against a non-Unicode code point (not just the arguable ones), and it skips the optimizations that might cause the warning to not be output. (It currently still won&#39;t warn if the match isn&#39;t even attempted, like in the <code>&quot;foobar&quot;</code> example above.)</p>

<p>In summary, Perl now normally treats non-Unicode code points as typical Unicode unassigned code points for regular expression matches, raising a warning only when it is arguable what the result should be. However, if this warning has been made fatal, it isn&#39;t skipped.</p>

<p>There is one exception to all this. <code>\p{All}</code> looks like a Unicode property, but it is a Perl extension that is defined to be true for all possible code points, Unicode or not, so no warning is ever generated when matching this against a non-Unicode code point. (Prior to v5.20, it was an exact synonym for <code>\p{Any}</code>, matching code points <code>0</code> through <code>0x10FFFF</code>.)</p>

<h2 id="Security-Implications-of-Unicode"><a class="permalink" href="perlunicode.html#Security-Implications-of-Unicode">#</a><a id="Security"></a>Security Implications of Unicode</h2>

<p>First, read <a href="https://www.unicode.org/reports/tr36">Unicode Security Considerations</a>.</p>

<p>Also, note the following:</p>

<ul>

<li><p>Malformed UTF-8</p>

<p>UTF-8 is very structured, so many combinations of bytes are invalid. In the past, Perl tried to soldier on and make some sense of invalid combinations, but this can lead to security holes, so now, if the Perl core needs to process an invalid combination, it will either raise a fatal error, or will replace those bytes by the sequence that forms the Unicode REPLACEMENT CHARACTER, for which purpose Unicode created it.</p>

<p>Every code point can be represented by more than one possible syntactically valid UTF-8 sequence. Early on, both Unicode and Perl considered any of these to be valid, but now, all sequences longer than the shortest possible one are considered to be malformed.</p>

<p>Unicode considers many code points to be illegal, or to be avoided. Perl generally accepts them, once they have passed through any input filters that may try to exclude them. These have been discussed above (see &quot;Surrogates&quot; under UTF-16 in <a href="perlunicode.html#Unicode-Encodings">&quot;Unicode Encodings&quot;</a>, <a href="perlunicode.html#Noncharacter-code-points">&quot;Noncharacter code points&quot;</a>, and <a href="perlunicode.html#Beyond-Unicode-code-points">&quot;Beyond Unicode code points&quot;</a>).</p>

</li>
<li><p>Regular expression pattern matching may surprise you if you&#39;re not accustomed to Unicode. Starting in Perl 5.14, several pattern modifiers are available to control this, called the character set modifiers. Details are given in <a href="perlre.html#Character-set-modifiers">&quot;Character set modifiers&quot; in perlre</a>.</p>

</li>
</ul>

<p>As discussed elsewhere, Perl has one foot (two hooves?) planted in each of two worlds: the old world of ASCII and single-byte locales, and the new world of Unicode, upgrading when necessary. If your legacy code does not explicitly use Unicode, no automatic switch-over to Unicode should happen.</p>

<h2 id="Unicode-in-Perl-on-EBCDIC"><a class="permalink" href="perlunicode.html#Unicode-in-Perl-on-EBCDIC">#</a><a id="Unicode3"></a>Unicode in Perl on EBCDIC</h2>

<p>Unicode is supported on EBCDIC platforms. See <a href="perlebcdic.html">perlebcdic</a>.</p>

<p>Unless ASCII vs. EBCDIC issues are specifically being discussed, references to UTF-8 encoding in this document and elsewhere should be read as meaning UTF-EBCDIC on EBCDIC platforms. See <a href="perlebcdic.html#Unicode-and-UTF">&quot;Unicode and UTF&quot; in perlebcdic</a>.</p>

<p>Because UTF-EBCDIC is so similar to UTF-8, the differences are mostly hidden from you; <span style="white-space: nowrap;"><code>use utf8</code></span> (and NOT something like <span style="white-space: nowrap;"><code>use utfebcdic</code></span>) declares the script is in the platform&#39;s &quot;native&quot; 8-bit encoding of Unicode. (Similarly for the <code>&quot;:utf8&quot;</code> layer.)</p>

<h2 id="Locales"><a class="permalink" href="perlunicode.html#Locales">#</a>Locales</h2>

<p>See <a href="perllocale.html#Unicode-and-UTF-8">&quot;Unicode and UTF-8&quot; in perllocale</a></p>

<h2 id="When-Unicode-Does-Not-Happen"><a class="permalink" href="perlunicode.html#When-Unicode-Does-Not-Happen">#</a><a id="When"></a>When Unicode Does Not Happen</h2>

<p>There are still many places where Unicode (in some encoding or another) could be given as arguments or received as results, or both in Perl, but it is not, in spite of Perl having extensive ways to input and output in Unicode, and a few other &quot;entry points&quot; like the <code>@ARGV</code> array (which can sometimes be interpreted as UTF-8).</p>

<p>The following are such interfaces. Also, see <a href="perlunicode.html#The-%22Unicode-Bug%22">&quot;The &quot;Unicode Bug&quot;&quot;</a>. For all of these interfaces Perl currently (as of v5.16.0) simply assumes byte strings both as arguments and results, or UTF-8 strings if the (deprecated) <code>encoding</code> pragma has been used.</p>

<p>One reason that Perl does not attempt to resolve the role of Unicode in these situations is that the answers are highly dependent on the operating system and the file system(s). For example, whether filenames can be in Unicode and in exactly what kind of encoding, is not exactly a portable concept. Similarly for <code>qx</code> and <code>system</code>: how well will the &quot;command-line interface&quot; (and which of them?) handle Unicode?</p>

<ul>

<li><p><code>chdir</code>, <code>chmod</code>, <code>chown</code>, <code>chroot</code>, <code>exec</code>, <code>link</code>, <code>lstat</code>, <code>mkdir</code>, <code>rename</code>, <code>rmdir</code>, <code>stat</code>, <code>symlink</code>, <code>truncate</code>, <code>unlink</code>, <code>utime</code>, <code>-X</code></p>

</li>
<li><p><code>%ENV</code></p>

</li>
<li><p><code>glob</code> (aka the <code>&lt;*&gt;</code>)</p>

</li>
<li><p><code>open</code>, <code>opendir</code>, <code>sysopen</code></p>

</li>
<li><p><code>qx</code> (aka the backtick operator), <code>system</code></p>

</li>
<li><p><code>readdir</code>, <code>readlink</code></p>

</li>
</ul>

<h2 id="The-&quot;Unicode-Bug&quot;"><a class="permalink" href="perlunicode.html#The-%22Unicode-Bug%22">#</a><a id="The"></a><a id="The-Unicode-Bug"></a>The &quot;Unicode Bug&quot;</h2>

<p>The term, &quot;Unicode bug&quot; has been applied to an inconsistency with the code points in the <code>Latin-1 Supplement</code> block, that is, between 128 and 255. Without a locale specified, unlike all other characters or code points, these characters can have very different semantics depending on the rules in effect. (Characters whose code points are above 255 force Unicode rules; whereas the rules for ASCII characters are the same under both ASCII and Unicode rules.)</p>

<p>Under Unicode rules, these upper-Latin1 characters are interpreted as Unicode code points, which means they have the same semantics as Latin-1 (ISO-8859-1) and C1 controls.</p>

<p>As explained in <a href="perlunicode.html#ASCII-Rules-versus-Unicode-Rules">&quot;ASCII Rules versus Unicode Rules&quot;</a>, under ASCII rules, they are considered to be unassigned characters.</p>

<p>This can lead to unexpected results. For example, a string&#39;s semantics can suddenly change if a code point above 255 is appended to it, which changes the rules from ASCII to Unicode. As an example, consider the following program and its output:</p>

<pre><code>$ perl -le&#39;
    no feature &quot;unicode_strings&quot;;
    $s1 = &quot;\xC2&quot;;
    $s2 = &quot;\x{2660}&quot;;
    for ($s1, $s2, $s1.$s2) {
        print /\w/ || 0;
    }
&#39;
0
0
1</code></pre>

<p>If there&#39;s no <code>\w</code> in <code>s1</code> nor in <code>s2</code>, why does their concatenation have one?</p>

<p>This anomaly stems from Perl&#39;s attempt to not disturb older programs that didn&#39;t use Unicode, along with Perl&#39;s desire to add Unicode support seamlessly. But the result turned out to not be seamless. (By the way, you can choose to be warned when things like this happen. See <code><a href="./encoding::warnings.html">encoding::warnings</a></code>.)</p>

<p><a href="feature.html#The-%27unicode_strings%27-feature"><span style="white-space: nowrap;"><code>use feature &#39;unicode_strings&#39;</code></span></a> was added, starting in Perl v5.12, to address this problem. It affects these things:</p>

<ul>

<li><p>Changing the case of a scalar, that is, using <code>uc()</code>, <code>ucfirst()</code>, <code>lc()</code>, and <code>lcfirst()</code>, or <code>\L</code>, <code>\U</code>, <code>\u</code> and <code>\l</code> in double-quotish contexts, such as regular expression substitutions.</p>

<p>Under <code>unicode_strings</code> starting in Perl 5.12.0, Unicode rules are generally used. See <a href="perlfunc.html#lc">&quot;lc&quot; in perlfunc</a> for details on how this works in combination with various other pragmas.</p>

</li>
<li><p>Using caseless (<code>/i</code>) regular expression matching.</p>

<p>Starting in Perl 5.14.0, regular expressions compiled within the scope of <code>unicode_strings</code> use Unicode rules even when executed or compiled into larger regular expressions outside the scope.</p>

</li>
<li><p>Matching any of several properties in regular expressions.</p>

<p>These properties are <code>\b</code> (without braces), <code>\B</code> (without braces), <code>\s</code>, <code>\S</code>, <code>\w</code>, <code>\W</code>, and all the Posix character classes <i>except</i> <code>[[:ascii:]]</code>.</p>

<p>Starting in Perl 5.14.0, regular expressions compiled within the scope of <code>unicode_strings</code> use Unicode rules even when executed or compiled into larger regular expressions outside the scope.</p>

</li>
<li><p>In <code>quotemeta</code> or its inline equivalent <code>\Q</code>.</p>

<p>Starting in Perl 5.16.0, consistent quoting rules are used within the scope of <code>unicode_strings</code>, as described in <a href="perlfunc.html#quotemeta">&quot;quotemeta&quot; in perlfunc</a>. Prior to that, or outside its scope, no code points above 127 are quoted in UTF-8 encoded strings, but in byte encoded strings, code points between 128-255 are always quoted.</p>

</li>
<li><p>In the <code>..</code> or <a href="perlop.html#Range-Operators">range</a> operator.</p>

<p>Starting in Perl 5.26.0, the range operator on strings treats their lengths consistently within the scope of <code>unicode_strings</code>. Prior to that, or outside its scope, it could produce strings whose length in characters exceeded that of the right-hand side, where the right-hand side took up more bytes than the correct range endpoint.</p>

</li>
<li><p>In <a href="perlfunc.html#split"><code>split</code>&#39;s special-case whitespace splitting</a>.</p>

<p>Starting in Perl 5.28.0, the <code>split</code> function with a pattern specified as a string containing a single space handles whitespace characters consistently within the scope of <code>unicode_strings</code>. Prior to that, or outside its scope, characters that are whitespace according to Unicode rules but not according to ASCII rules were treated as field contents rather than field separators when they appear in byte-encoded strings.</p>

</li>
</ul>

<p>You can see from the above that the effect of <code>unicode_strings</code> increased over several Perl releases. (And Perl&#39;s support for Unicode continues to improve; it&#39;s best to use the latest available release in order to get the most complete and accurate results possible.) Note that <code>unicode_strings</code> is automatically chosen if you <span style="white-space: nowrap;"><code>use 5.012</code></span> or higher.</p>

<p>For Perls earlier than those described above, or when a string is passed to a function outside the scope of <code>unicode_strings</code>, see the next section.</p>

<h2 id="Forcing-Unicode-in-Perl-(Or-Unforcing-Unicode-in-Perl)"><a class="permalink" href="perlunicode.html#Forcing-Unicode-in-Perl-(Or-Unforcing-Unicode-in-Perl)">#</a><a id="Forcing"></a><a id="Forcing-Unicode-in-Perl-Or-Unforcing-Unicode-in-Perl"></a>Forcing Unicode in Perl (Or Unforcing Unicode in Perl)</h2>

<p>Sometimes (see <a href="perlunicode.html#When-Unicode-Does-Not-Happen">&quot;When Unicode Does Not Happen&quot;</a> or <a href="perlunicode.html#The-%22Unicode-Bug%22">&quot;The &quot;Unicode Bug&quot;&quot;</a>) there are situations where you simply need to force a byte string into UTF-8, or vice versa. The standard module <a href="Encode.html">Encode</a> can be used for this, or the low-level calls <a href="utf8.html#Utility-functions"><code>utf8::upgrade($bytestring)</code></a> and <a href="utf8.html#Utility-functions"><code>utf8::downgrade($utf8string[, FAIL_OK])</code></a>.</p>

<p>Note that <code>utf8::downgrade()</code> can fail if the string contains characters that don&#39;t fit into a byte.</p>

<p>Calling either function on a string that already is in the desired state is a no-op.</p>

<p><a href="perlunicode.html#ASCII-Rules-versus-Unicode-Rules">&quot;ASCII Rules versus Unicode Rules&quot;</a> gives all the ways that a string is made to use Unicode rules.</p>

<h2 id="Using-Unicode-in-XS"><a class="permalink" href="perlunicode.html#Using-Unicode-in-XS">#</a><a id="Using"></a>Using Unicode in XS</h2>

<p>See <a href="perlguts.html#Unicode-Support">&quot;Unicode Support&quot; in perlguts</a> for an introduction to Unicode at the XS level, and <a href="perlapi.html#Unicode-Support">&quot;Unicode Support&quot; in perlapi</a> for the API details.</p>

<h2 id="Hacking-Perl-to-work-on-earlier-Unicode-versions-(for-very-serious-hackers-only)"><a class="permalink" href="perlunicode.html#Hacking-Perl-to-work-on-earlier-Unicode-versions-(for-very-serious-hackers-only)">#</a><a id="Hacking"></a><a id="Hacking-Perl-to-work-on-earlier-Unicode-versions-for-very-serious-hackers-only"></a>Hacking Perl to work on earlier Unicode versions (for very serious hackers only)</h2>

<p>Perl by default comes with the latest supported Unicode version built-in, but the goal is to allow you to change to use any earlier one. In Perls v5.20 and v5.22, however, the earliest usable version is Unicode 5.1. Perl v5.18 and v5.24 are able to handle all earlier versions.</p>

<p>Download the files in the desired version of Unicode from the Unicode web site <a href="https://www.unicode.org">https://www.unicode.org</a>). These should replace the existing files in <i>lib/unicore</i> in the Perl source tree. Follow the instructions in <i>README.perl</i> in that directory to change some of their names, and then build perl (see <a href="INSTALL.html">INSTALL</a>).</p>

<h2 id="Porting-code-from-perl-5.6.X"><a class="permalink" href="perlunicode.html#Porting-code-from-perl-5.6.X">#</a><a id="Porting"></a>Porting code from perl-5.6.X</h2>

<p>Perls starting in 5.8 have a different Unicode model from 5.6. In 5.6 the programmer was required to use the <code>utf8</code> pragma to declare that a given scope expected to deal with Unicode data and had to make sure that only Unicode data were reaching that scope. If you have code that is working with 5.6, you will need some of the following adjustments to your code. The examples are written such that the code will continue to work under 5.6, so you should be safe to try them out.</p>

<ul>

<li><p>A filehandle that should read or write UTF-8</p>

<pre><code>if ($] &gt; 5.008) {
  binmode $fh, &quot;:encoding(UTF-8)&quot;;
}</code></pre>

</li>
<li><p>A scalar that is going to be passed to some extension</p>

<p>Be it <code>Compress::Zlib</code>, <code>Apache::Request</code> or any extension that has no mention of Unicode in the manpage, you need to make sure that the UTF8 flag is stripped off. Note that at the time of this writing (January 2012) the mentioned modules are not UTF-8-aware. Please check the documentation to verify if this is still true.</p>

<pre><code>if ($] &gt; 5.008) {
  require Encode;
  $val = Encode::encode(&quot;UTF-8&quot;, $val); # make octets
}</code></pre>

</li>
<li><p>A scalar we got back from an extension</p>

<p>If you believe the scalar comes back as UTF-8, you will most likely want the UTF8 flag restored:</p>

<pre><code>if ($] &gt; 5.008) {
  require Encode;
  $val = Encode::decode(&quot;UTF-8&quot;, $val);
}</code></pre>

</li>
<li><p>Same thing, if you are really sure it is UTF-8</p>

<pre><code>if ($] &gt; 5.008) {
  require Encode;
  Encode::_utf8_on($val);
}</code></pre>

</li>
<li><p>A wrapper for <a href="DBI.html">DBI</a> <code>fetchrow_array</code> and <code>fetchrow_hashref</code></p>

<p>When the database contains only UTF-8, a wrapper function or method is a convenient way to replace all your <code>fetchrow_array</code> and <code>fetchrow_hashref</code> calls. A wrapper function will also make it easier to adapt to future enhancements in your database driver. Note that at the time of this writing (January 2012), the DBI has no standardized way to deal with UTF-8 data. Please check the <a href="DBI.html">DBI documentation</a> to verify if that is still true.</p>

<pre><code>sub fetchrow {
  # $what is one of fetchrow_{array,hashref}
  my($self, $sth, $what) = @_;
  if ($] &lt; 5.008) {
    return $sth-&gt;$what;
  } else {
    require Encode;
    if (wantarray) {
      my @arr = $sth-&gt;$what;
      for (@arr) {
        defined &amp;&amp; /[^\000-\177]/ &amp;&amp; Encode::_utf8_on($_);
      }
      return @arr;
    } else {
      my $ret = $sth-&gt;$what;
      if (ref $ret) {
        for my $k (keys %$ret) {
          defined
          &amp;&amp; /[^\000-\177]/
          &amp;&amp; Encode::_utf8_on($_) for $ret-&gt;{$k};
        }
        return $ret;
      } else {
        defined &amp;&amp; /[^\000-\177]/ &amp;&amp; Encode::_utf8_on($_) for $ret;
        return $ret;
      }
    }
  }
}</code></pre>

</li>
<li><p>A large scalar that you know can only contain ASCII</p>

<p>Scalars that contain only ASCII and are marked as UTF-8 are sometimes a drag to your program. If you recognize such a situation, just remove the UTF8 flag:</p>

<pre><code>utf8::downgrade($val) if $] &gt; 5.008;</code></pre>

</li>
</ul>

<h1 id="BUGS"><a class="permalink" href="perlunicode.html#BUGS">#</a>BUGS</h1>

<p>See also <a href="perlunicode.html#The-%22Unicode-Bug%22">&quot;The &quot;Unicode Bug&quot;&quot;</a> above.</p>

<h2 id="Interaction-with-Extensions"><a class="permalink" href="perlunicode.html#Interaction-with-Extensions">#</a><a id="Interaction"></a>Interaction with Extensions</h2>

<p>When Perl exchanges data with an extension, the extension should be able to understand the UTF8 flag and act accordingly. If the extension doesn&#39;t recognize that flag, it&#39;s likely that the extension will return incorrectly-flagged data.</p>

<p>So if you&#39;re working with Unicode data, consult the documentation of every module you&#39;re using if there are any issues with Unicode data exchange. If the documentation does not talk about Unicode at all, suspect the worst and probably look at the source to learn how the module is implemented. Modules written completely in Perl shouldn&#39;t cause problems. Modules that directly or indirectly access code written in other programming languages are at risk.</p>

<p>For affected functions, the simple strategy to avoid data corruption is to always make the encoding of the exchanged data explicit. Choose an encoding that you know the extension can handle. Convert arguments passed to the extensions to that encoding and convert results back from that encoding. Write wrapper functions that do the conversions for you, so you can later change the functions when the extension catches up.</p>

<p>To provide an example, let&#39;s say the popular <code>Foo::Bar::escape_html</code> function doesn&#39;t deal with Unicode data yet. The wrapper function would convert the argument to raw UTF-8 and convert the result back to Perl&#39;s internal representation like so:</p>

<pre><code>sub my_escape_html ($) {
    my($what) = shift;
    return unless defined $what;
    Encode::decode(&quot;UTF-8&quot;, Foo::Bar::escape_html(
                                 Encode::encode(&quot;UTF-8&quot;, $what)));
}</code></pre>

<p>Sometimes, when the extension does not convert data but just stores and retrieves it, you will be able to use the otherwise dangerous <a href="Encode.html#_utf8_on"><code>Encode::_utf8_on()</code></a> function. Let&#39;s say the popular <code>Foo::Bar</code> extension, written in C, provides a <code>param</code> method that lets you store and retrieve data according to these prototypes:</p>

<pre><code>$self-&gt;param($name, $value);            # set a scalar
$value = $self-&gt;param($name);           # retrieve a scalar</code></pre>

<p>If it does not yet provide support for any encoding, one could write a derived class with such a <code>param</code> method:</p>

<pre><code>sub param {
  my($self,$name,$value) = @_;
  utf8::upgrade($name);     # make sure it is UTF-8 encoded
  if (defined $value) {
    utf8::upgrade($value);  # make sure it is UTF-8 encoded
    return $self-&gt;SUPER::param($name,$value);
  } else {
    my $ret = $self-&gt;SUPER::param($name);
    Encode::_utf8_on($ret); # we know, it is UTF-8 encoded
    return $ret;
  }
}</code></pre>

<p>Some extensions provide filters on data entry/exit points, such as <code>DB_File::filter_store_key</code> and family. Look out for such filters in the documentation of your extensions; they can make the transition to Unicode data much easier.</p>

<h2 id="Speed"><a class="permalink" href="perlunicode.html#Speed">#</a>Speed</h2>

<p>Some functions are slower when working on UTF-8 encoded strings than on byte encoded strings. All functions that need to hop over characters such as <code>length()</code>, <code>substr()</code> or <code>index()</code>, or matching regular expressions can work <b>much</b> faster when the underlying data are byte-encoded.</p>

<p>In Perl 5.8.0 the slowness was often quite spectacular; in Perl 5.8.1 a caching scheme was introduced which improved the situation. In general, operations with UTF-8 encoded strings are still slower. As an example, the Unicode properties (character classes) like <code>\p{Nd}</code> are known to be quite a bit slower (5-20 times) than their simpler counterparts like <code>[0-9]</code> (then again, there are hundreds of Unicode characters matching <code>Nd</code> compared with the 10 ASCII characters matching <code>[0-9]</code>).</p>

<h1 id="SEE-ALSO"><a class="permalink" href="perlunicode.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p><a href="perlunitut.html">perlunitut</a>, <a href="perluniintro.html">perluniintro</a>, <a href="perluniprops.html">perluniprops</a>, <a href="Encode.html">Encode</a>, <a href="open.html">open</a>, <a href="utf8.html">utf8</a>, <a href="bytes.html">bytes</a>, <a href="perlretut.html">perlretut</a>, <a href="perlvar.html#%24%7B%5EUNICODE%7D">&quot;${^UNICODE}&quot; in perlvar</a>, <a href="https://www.unicode.org/reports/tr44">https://www.unicode.org/reports/tr44</a>).</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
